﻿Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Timers
Imports HslCommunication
Imports HslCommunication.LogNet
Imports HslCommunication.Profinet.Siemens

Public Class frmRGV_JBGW
    '接驳工位DataRow的行号  _1FKX_ROW,_1GKX_3_ROW
    Const _1FKX_ROW As Integer = 0
    Const _1FMS_ROW As Integer = 1

    Const _1GMS1_1_ROW As Integer = 2
    Const _1GKX_1_ROW As Integer = 3
    Const _1GMS1_2_ROW As Integer = 4

    Const _1GMS_3_ROW As Integer = 5
    Const _1GKX_3_ROW As Integer = 6

    Const _2FKS_ROW As Integer = 7
    Const _2FMX_ROW As Integer = 8
    Const _2GMX_ROW As Integer = 9
    Const _2GKS_ROW As Integer = 10

    Const RGV_ERROR_1 As String = "1: 上下料时，车门异常关闭，请联系设备管理员。"
    Const RGV_ERROR_2 As String = "2: 上下料前，RGV车门打不开。请联系设备管理员。"

    Private _Server_Run As Boolean = False '监督
    Private _Active_Run As Boolean = False '动作
    Private Actioning As Boolean = False '是否在动作中

    '接驳工位PLC控件
    Private plc_1_FKS As SiemensS7Net '法兰板空框上料
    Private plc_1_FLB As SiemensS7Net
    Private plc_1_GJB2 As SiemensS7Net
    Private plc_1_GJB3 As SiemensS7Net
    Private plc_2 As SiemensS7Net

    'RGV的PLC控件
    Private plc_car1 As SiemensS7Net
    Private plc_car2 As SiemensS7Net

    '预组装工位-需要的法兰板及骨架板数量
    Dim GJB_Need_Count As Integer
    Dim FLB_Need_Count As Integer

    '线程
    'Private th_car1 As Threading.Thread
    'Private th_car2 As Threading.Thread
    'Private th_JBGW As Threading.Thread

    Private th_JBGW_RGV As Threading.Thread
    Private th_Main As Threading.Thread

    'PLC的连接状态
    Public plc_1_FKS_Connected As Boolean = False '法兰板空框上料
    Public plc_1_FLB_Connected As Boolean = False
    Public plc_1_GJB3_Connected As Boolean = False
    Public plc_1_GJB2_Connected As Boolean = False
    Public plc_2_Connected As Boolean = False

    '接驳工位（以PLC为单位）的手动/自动状态
    Public plc_1_FLB_AutoMode As Boolean = False
    Public plc_1_GJB3_AutoMode As Boolean = False
    Public plc_1_GJB2_AutoMode As Boolean = False
    Public plc_2F_AutoMode As Boolean = False
    Public plc_2G_AutoMode As Boolean = False

    Private WithEvents tmHMI As New Windows.Forms.Timer
    Private WithEvents tm As New Timers.Timer
    'other
    Private _1FMS_READER_IP As String = "127.0.0.1" '1车间的读码器的IP
    Private g_szInitFileName As String    'ini文件
    Private GJB_TYPE_IS_EQUALS As Boolean '1、2车间是否相同的型号
    Private GJB_BOX_TYPE_EQUALS As Boolean '骨架板是否可兼容
    Private logfile As ILogNet            'log文件
    Private taskNo As String              '任务号
    Private taskWaitTimeOut As Integer = 5 * 60 * 1000 '任务等待时长
    Private GJB_LK_count_in_Car1 As Integer = 0 '1车上的料框数--新建任务时使用
    Private FLB_LK_count_in_Car1 As Integer = 0 '1车上的料框数--新建任务时使用
    Private GJB_LK_count_in_Car2 As Integer = 0 '2车上的料框数--新建任务时使用
    Private FLB_LK_count_in_Car2 As Integer = 0 '2车上的料框数--新建任务时使用

    Enum TASK_STATUS As Integer
        wait = 0  '等待
        begin = 1 '开始
        completed = 2 '单任务完成
        remoteCarStop = 3 '远程RGV急停
        carStop = 4 'RGV本地急停
        remoteStationStop = 5 '接驳工位远程急停
        stationStop = 6 '接驳工位本地急停
        carDoorError = 7 '车门出错

        WorkShop1DoorError = 8 '1车间门出错
        WorkShop2DoorError = 9 '1车间门出错

        carDNerror = 10 'RGV 下料错误
        carUPerror = 11 'RGV 上料错误
        stationDNerror = 12 '接驳工位 下料错误
        stationUPerror = 13 '接驳工位 上料错误

        carDNcompleted = 14      'RGV 下料完成
        stationDNcompleted = 15  '接驳工位 下料完成
        carUPcompleted = 16      'RGV 上料完成
        stationUPcompleted = 17  '接驳工位 上料完成

        carMoveError = 18        'RGV 移动出错
        stationAllowError = 19   '接驳工位 允许上下料出错 
        rasterAllowError = 20    '光栅被档--有料框在线和车之间
        'carWaitTimeOut = 20      'RGV 动作超时
        'stationWaitTimeOut = 21  '接驳工位 动作超时
        Cancel = 98

        otherError = 99          '其它错误
        AllCompleted = 100       '全部任务完成
    End Enum
    Public Sub New()
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub
    Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
        Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
        dllName = dllName.Replace(".", "_")
        If dllName.EndsWith("_resources") Then
            Return Nothing
        End If
        Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
        Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
        Return System.Reflection.Assembly.Load(bytes)
    End Function
    Private Sub frmRGV_JBGW_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
        If Not HslCommunication.Authorization.SetAuthorizationCode("e3e27344-1a9f-4d8b-b4fc-da5cae4b99bf") Then

        End If

        Me.g_szInitFileName = My.Application.Info.DirectoryPath & "\RGV.ini"
        logfile = New LogNetDateTime(Application.StartupPath & "\\Logs", GenerateMode.ByEveryDay)
        'createRow()
        connectDB()
        'nudGJBnum.Value = getSysParam("YZZ_GJB_BOX_Num")
        lblsysGJBnum.Text = getSysParam("YZZ_GJB_BOX_Num")
        _1FMS_READER_IP = getFLB_1FMS_Reader_IP()

        '预组装工位需要的法兰板和骨架板料框数
        GJB_Need_Count = getSysParam("GJB_need_Count_For_YZZ")
        'NudGJBNeed.Value = GJB_Need_Count
        lblsysGJBnum.Text = GJB_Need_Count
        FLB_Need_Count = getSysParam("FLB_need_Count_For_YZZ")
        'nudFLBNeed.Value = FLB_Need_Count
        lblsysFLBNeed.Text = FLB_Need_Count

        '-----------任务-----------
        setTaskAndTaskDetail()
        If IsNothing(dtTask) = False AndAlso dtTask.Rows.Count > 0 Then
            Actioning = True
        End If

        '--------------------------

        '车1
        'th_car1 = New Threading.Thread(AddressOf readCar1)
        'th_car1.Start()
        ''车2
        'th_car2 = New Threading.Thread(AddressOf readCar2)
        'th_car2.Start()
        ''接驳工位
        'th_JBGW = New Threading.Thread(AddressOf Read_JBGW_PLC)
        'th_JBGW.Start()

        th_JBGW_RGV = New Thread(AddressOf getJBGW_RGV_DATA)
        th_JBGW_RGV.Start()

        '主线程
        th_Main = New Threading.Thread(AddressOf MainThread)
        th_Main.Start()

        'tmHMI.Interval = 200
        'tmHMI.Start()

        'tm.Interval = 500
        'tm.Start()

    End Sub

    Delegate Sub getJBGW_data_Det()
    Delegate Sub getRGV_data_Det()
    Private getJBGW_data_caller As New getJBGW_data_Det(AddressOf setJBGW_Data)
    Private getRGV_data_caller As New getRGV_data_Det(AddressOf setRGV_Data)

    Private Sub getJBGW_RGV_DATA()
        Dim carIp1 As String = GetIniFile("IP", "CAR1", "", Me.g_szInitFileName)
        Dim carIp2 As String = GetIniFile("IP", "CAR2", "", Me.g_szInitFileName)
        Dim _ip_1FKS As String = GetIniFile("JBGW", "1FKS", "", Me.g_szInitFileName)
        Dim _ip_1F As String = GetIniFile("JBGW", "1F", "", Me.g_szInitFileName)
        Dim _ip_1G3 As String = GetIniFile("JBGW", "1G3", "", Me.g_szInitFileName)
        Dim _ip_1G2 As String = GetIniFile("JBGW", "1G2", "", Me.g_szInitFileName)
        Dim _ip_2 As String = GetIniFile("JBGW", "2G", "", Me.g_szInitFileName)

        plc_car1 = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_car1

            .IpAddress = carIp1
            .Port = 102
            .SetPersistentConnection()
        End With

        plc_car2 = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_car2
            .IpAddress = carIp2
            .Port = 102
            .SetPersistentConnection()
        End With

        plc_1_FLB = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_1_FLB
            .IpAddress = _ip_1F
            .Port = 102
            .SetPersistentConnection()
            'If plc_1_FLB_Connected Then Read_1FLB_1GJB3_PLC(plc_1_FLB, dtJBGWName.Rows(_1FKX_ROW), dtJBGWName.Rows(_1FMS_ROW), 11)

        End With
        plc_1_GJB3 = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_1_GJB3
            .IpAddress = _ip_1G3
            .Port = 102
            .SetPersistentConnection()
            'If plc_1_GJB3_Connected Then Read_1FLB_1GJB3_PLC(plc_1_GJB3, dtJBGWName.Rows(_1GKX_3_ROW), dtJBGWName.Rows(_1GMS_3_ROW), 13)
        End With
        plc_1_GJB2 = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_1_GJB2
            .IpAddress = _ip_1G2
            .Port = 102
            .SetPersistentConnection()
            'If plc_1_GJB2_Connected Then Read_1GJB2_PLC(plc_1_GJB2, dtJBGWName.Rows(_1GKX_1_ROW), dtJBGWName.Rows(_1GMS1_1_ROW), dtJBGWName.Rows(_1GMS1_2_ROW), 12)
        End With

        plc_2 = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_2
            .IpAddress = _ip_2
            .Port = 102
            .SetPersistentConnection()
            'If plc_2_Connected Then Read_2_PLC(plc_2, dtJBGWName.Rows(_2FMX_ROW), dtJBGWName.Rows(_2FKS_ROW), dtJBGWName.Rows(_2GMX_ROW), dtJBGWName.Rows(_2GKS_ROW), 21)
        End With

        plc_1_FKS = New SiemensS7Net(SiemensPLCS.S1200)
        With plc_1_FKS
            .IpAddress = _ip_1FKS
            .Port = 102
            .SetPersistentConnection()
            'If plc_1_FLB_Connected Then Read_1FLB_1GJB3_PLC(plc_1_FLB, dtJBGWName.Rows(_1FKX_ROW), dtJBGWName.Rows(_1FMS_ROW), 11)

        End With

        While _Server_Run
            Me.Invoke(getJBGW_data_caller)
            Me.Invoke(getRGV_data_caller)

            plc_2_Connected = plc_2.ReadBool("DB200.DBX111.1").IsSuccess()
            plc_1_GJB2_Connected = plc_1_GJB2.ReadBool("DB15.DBX0.6").IsSuccess()
            plc_1_GJB3_Connected = plc_1_GJB3.ReadBool("DB1.DBX0.5").IsSuccess()
            plc_1_FLB_Connected = plc_1_FLB.ReadBool("DB1.DBX0.5").IsSuccess()
            plc_1_FKS_Connected = plc_1_FKS.ReadInt16("DB30.DBX2.0").IsSuccess
            plc_2F_AutoMode = gw2FKS.controlmode
            plc_2G_AutoMode = gw2GKS.controlmode
            plc_1_FLB_AutoMode = gw1FMS.controlmode
            plc_1_GJB3_AutoMode = gw1GMS_3.controlmode
            plc_1_GJB2_AutoMode = gw1GMS1_2.controlmode


            Thread.Sleep(500)
        End While

    End Sub

    Private Sub MainThread()

        While _Active_Run
            '系统设定参数
            lblsysGJBnum.Text = getSysParam("YZZ_GJB_BOX_Num")
            lblsysGJBNeed.Text = getSysParam("GJB_need_Count_For_YZZ")
            lblsysFLBNeed.Text = getSysParam("FLB_need_Count_For_YZZ")
            If getSysParam("RGV_AUTO_OPEN") = "1" And getSysParam("RGV_AUTO_TASK_PASUE") = "0" Then

                '进行任务
                If chkTaskOnly.Checked = False Then
                    start_up_Task()
                End If

                getTaskAndTaskDetail()

                '如有未完成的任务，则不行程新的任务
                If Actioning = False Then

                    '骨架板2 和 预组装是否一致
                    GJB_TYPE_IS_EQUALS = Equals_GJB2_YZZ()

                    '自动模式 逻辑判断
                    '1）如果2车间都是自动模式才可以使用
                    '2）上记1为True后，如两车间类型相同，则检查1G2是否自动模式，如果1G2是手动模式但如果1G3是自动模式，以后的任务就去1G3
                    '3）上记1为True后，如两车间类型不同，则1G3是否是自动模式
                    'If plc_2F_AutoMode = True And plc_2G_AutoMode = True Then '2车间 是否都是自动模式
                    '    'If plc_1_FLB_AutoMode = False Then '1车间法兰板是否是自动模式
                    '    '    GoTo BeforeWhileEnd
                    '    'End If
                    '    '1，2车间 类型 相同
                    '    If GJB_TYPE_IS_EQUALS Then
                    '        '1G2是否自动模式
                    '        If plc_1_GJB2_AutoMode Then
                    '        Else
                    '            '如果1G2不能使用，1G3能使用也可以
                    '            If plc_1_GJB3_AutoMode = False Then
                    '                GoTo BeforeWhileEnd
                    '            End If
                    '        End If
                    '    Else '1，2车间 类型 不同
                    '        '1G3是否自动模式
                    '        If plc_1_GJB3_AutoMode Then
                    '        Else
                    '            GoTo BeforeWhileEnd
                    '        End If
                    '    End If

                    'Else
                    '    GoTo BeforeWhileEnd
                    'End If

                    '2车间的PLC能连接上才有意义===============================================
                    If plc_2_Connected = True Then

                        '====================== 各 种 数 据 准 备 ======================
                        '2车间法兰板、骨架板的空工位数量
                        Dim _2FMX_Empty_Count As Integer = 4 - DBNULLtoZero(dtJBGWName(_2FMX_ROW)("gwLKNum"))
                        Dim _2GMX_Empty_Count As Integer = 4 - DBNULLtoZero(dtJBGWName(_2GMX_ROW)("gwLKNum"))

                        '1车间法兰板的数量
                        Dim _1FMS_num As Integer = DBNULLtoZero(dtJBGWName(_1FMS_ROW)("gwLKNum"))
                        Dim _1FKX_num As Integer = DBNULLtoZero(dtJBGWName(_1FKX_ROW)("gwLKNum"))

                        '1GMS2的两个的数量 
                        Dim _1GMS1_1_num As Integer = DBNULLtoZero(dtJBGWName(_1GMS1_1_ROW)("gwLKNum"))
                        Dim _1GMS1_2_num As Integer = DBNULLtoZero(dtJBGWName(_1GMS1_2_ROW)("gwLKNum"))

                        '1GMS3的两个的数量 
                        Dim _1GMS_3_num As Integer = DBNULLtoZero(dtJBGWName(_1GMS_3_ROW)("gwLKNum"))


                        '2车间空框的数量
                        Dim _2FKS_Empty_num As Integer = DBNULLtoZero(dtJBGWName(_2FKS_ROW)("gwLKNum")) '2车间法兰板空框数量
                        Dim _2GKS_Empty_num As Integer = DBNULLtoZero(dtJBGWName(_2GKS_ROW)("gwLKNum")) '2车间骨架板空框数量
                        '============================================================

                        '单车、双车模式判断
                        Dim RGV_model As Integer = getRGVModel()
                        ''========================================================================-
                        If RGV_model = 1 Then 'RGV模式 1：单车模式
                            '★★★1★★★如果2车间法兰板缺1框，骨架板缺2框，并且 1车间，法兰板计划未完成,,骨架板未完成的时候
                            If _2FMX_Empty_Count >= 1 And _2GMX_Empty_Count >= 2 Then '2车间的空位
                                If FLB_Need_Count > 0 And _1FMS_num > 0 Then '法兰板是否准备好.如果法兰板已送完，就到★★★3★★★处理中
                                    If GJB_TYPE_IS_EQUALS And plc_1_GJB2_AutoMode = True Then '如果骨=预
                                        If (GJB_Need_Count > 0 And _1GMS1_1_num + _1GMS1_2_num > 0) Then  '有骨计划 并且 已准备好
                                            newTaskNo(1)
                                            active_RGV_model_1() '模式：1个法兰板，2个骨架板
                                        End If
                                    Else
                                        If (GJB_Need_Count > 0 And _1GMS_3_num > 0) Then  '有骨计划 并且 已准备好
                                            newTaskNo(1)
                                            active_RGV_model_1() '模式：1个法兰板，2个骨架板
                                        End If
                                    End If
                                End If
                            End If
                        End If
                        ''========================================================================-
                        If RGV_model = 2 Then 'RGV模式 2：同样双车模式 
                            '★★★2★★★如果2车间法兰板缺2框，骨架板缺2框，并且 1车间，法兰板计划未完成,骨架板未完成的时候
                            If _2FMX_Empty_Count >= 2 And _2GMX_Empty_Count > 2 And FLB_Need_Count > 0 And _1FMS_num >= 1 And GJB_Need_Count > 0 Then
                                newTaskNo(2)
                                active_RGV_model_2() '模式：1个法兰板，2个骨架板 X 2
                            End If
                        End If
                        ''========================================================================-
                        '★★★3★★★如果2车间骨架板缺>1框，并且 1车间，法兰板计划已完成,骨架板未完成的时候
                        FLB_Need_Count = getSysParam("FLB_need_Count_For_YZZ")
                        GJB_Need_Count = getSysParam("GJB_need_Count_For_YZZ")
                        If FLB_Need_Count = 0 And GJB_Need_Count > 0 Then '_2GMX_Empty_Count >= 1 And FLB_Need_Count = 0 Then

                            If GJB_TYPE_IS_EQUALS And plc_1_GJB2_AutoMode = True Then '如果骨=预
                                If (GJB_Need_Count > 0 And _1GMS1_1_num + _1GMS1_2_num > 0) Then '有骨计划 并且 已准备好
                                    newTaskNo(3)
                                    active_RGV_model_3() '模式：4个骨架板
                                ElseIf (GJB_Need_Count > 0 And _1GMS_3_num > 0) Then
                                    newTaskNo(3)
                                    active_RGV_model_3() '模式：4个骨架板
                                End If
                            Else
                                If (GJB_Need_Count > 0 And _1GMS_3_num > 0) Then '有骨计划 并且 已准备好
                                    newTaskNo(3)
                                    active_RGV_model_3() '模式：4个骨架板
                                End If
                            End If
                        End If
                        ''========================================================================-
                        '★★★4★★★如果2车间法兰板缺>1框，并且 1车间，法兰板计划未完成,骨架板已完成的时候
                        If GJB_Need_Count = 0 Then
                            If FLB_Need_Count > 0 And _2FMX_Empty_Count >= 1 And _1FMS_num > 0 Then
                                newTaskNo(4)
                                active_RGV_model_4() '模式：2个法兰板
                            End If

                        End If
                        ''========================================================================-
                        '追加计划已全部搬运完，只搬运空框的情况
                        If FLB_Need_Count = 0 And GJB_Need_Count = 0 Then '计划已全部完毕
                            Dim YZZ_GJB_BOX_Num As Integer = getSysParam("YZZ_GJB_BOX_Num") '线上除接驳工位的骨架板数量

                            '★★★5★★★如果有法兰板空框的时候，立刻搬运至1车间
                            If _2FKS_Empty_num >= 1 And YZZ_GJB_BOX_Num >= 0 And _2GKS_Empty_num > 0 Then
                                newTaskNo(5)
                                active_RGV_model_5() '模式5：1车，只装空框（1法2骨）
                            End If
                            If _2FKS_Empty_num > 0 And 2 - _1FKX_num > 0 Then
                                newTaskNo(4)
                                active_RGV_model_4() '模式：2个法兰板
                            End If
                            '★★★6★★★1车，只装骨架板空框
                            If _2FKS_Empty_num = 0 Then

                                If _2GKS_Empty_num >= 4 And get1CJkknum() > 0 And YZZ_GJB_BOX_Num > 0 Then '凑4框送走
                                    newTaskNo(6)
                                    active_RGV_model_6() '模式6：1车，只装骨架板空框
                                ElseIf YZZ_GJB_BOX_Num < 4 And _2GKS_Empty_num = YZZ_GJB_BOX_Num And _2GKS_Empty_num > 0 And get1CJkknum() > 0 Then
                                    newTaskNo(6)
                                    active_RGV_model_6() '模式6：1车，只装骨架板空框
                                End If

                            End If

                        End If

                    End If
                    ''========================================================================
                End If
            End If
BeforeWhileEnd: Threading.Thread.Sleep(200)
        End While
    End Sub
    Private Function get1CJkknum() As Integer
        Dim _1GKX_1_ROW_LKnum As Integer = 4 - dtJBGWName.Rows(_1GKX_1_ROW)("gwlknum")
        Dim _1GKX_3_ROW_LKnum As Integer = 4 - dtJBGWName.Rows(_1GKX_3_ROW)("gwlknum")

        If plc_1_GJB2_AutoMode = False Then
            _1GKX_1_ROW_LKnum = 0
        End If
        If plc_1_GJB3_AutoMode = False Then
            _1GKX_3_ROW_LKnum = 0
        End If

        If GJB_TYPE_IS_EQUALS = True Then
            Return _1GKX_1_ROW_LKnum + _1GKX_3_ROW_LKnum
        Else
            If GJB_BOX_TYPE_EQUALS Then
                Return _1GKX_1_ROW_LKnum + _1GKX_3_ROW_LKnum
            Else
                Return _1GKX_3_ROW_LKnum
            End If
        End If

    End Function
#Region "各种模式"

    'RGV模式 1：单车模式 
    Private Sub active_RGV_model_1()
        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer

        If select_Car(carPLC, carRow) Then
            If carRow = 0 Then
                one_Car_Model_in_One(carPLC, carRow)
            End If
            If carRow = 1 Then
                one_Car_Model_in_Two(carPLC, carRow)
            End If

        End If
    End Sub

    'RGV模式 2：同样双车模式 
    Private Sub active_RGV_model_2()
        '选车------------------
        Dim drCar1 As DataRow = dtCarName.Rows(0)
        Dim drCar2 As DataRow = dtCarName.Rows(1)
        Dim th_car2 As Threading.Thread
        If DBNULLtoBlank(drCar1("Communication")).ToLower = "true" And
            DBNULLtoBlank(drCar2("Communication")).ToLower = "true" Then
        Else
            active_RGV_model_1()
            Exit Sub
        End If
        If dtCarName(0)("CurrentLoc") = GetIniFile("Station", "CDW", "", g_szInitFileName) And
            dtCarName(1)("CurrentLoc") = GetIniFile("Station", "CDW", "", g_szInitFileName) Then

            th_car2 = New Threading.Thread(AddressOf th_car2_method)

            Full_FLB_to_RGV_in_one_3(plc_car1, 0)
            Full_GJB_to_RGV_in_one_5(plc_car1, 0)
            close_car_door(0)
            Full_to_JBGW_in_two_6(plc_car1, 0)
            Empty_to_RGV_in_two_1(plc_car1, 0)
            th_car2.Start()
            close_car_door(0)
            Empty_FLB_to_JBGW_in_one_2(plc_car1, 0)
            Empty_GJB_to_JBGW_in_one_4(plc_car1, 0)
            return_origin(0)
        End If
    End Sub

    'RGV模式 3：1车4个骨架板
    Private Sub active_RGV_model_3()

        '启动1辆车，只搬运骨架板（4个）	
        Mode3_Car2_action()
    End Sub

    'RGV模式 4：1车2个法兰板
    Private Sub active_RGV_model_4()

        '启动2辆车，分别搬运（1车运法兰板（2个），2车运骨架板（4个））
        mode3_Car1_action()
    End Sub

    'RGV模式 5：1车，只装空框（1法2骨）
    Private Sub active_RGV_model_5()
        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer

        If select_Car(carPLC, carRow) Then '选车
            Empty_to_RGV_in_two_1(carPLC, carRow)
            close_car_door(carRow)
            Empty_FLB_to_JBGW_in_one_2(carPLC, carRow)
            Empty_GJB_to_JBGW_in_one_4(carPLC, carRow)
            return_origin(carRow)
        End If
    End Sub

    'RGV模式 6：1车，只装骨架板空框
    Private Sub active_RGV_model_6()
        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer

        If select_Car(carPLC, carRow) Then '选车
            Mode3_GJB_UP_Empty_in_Two_1(carPLC, carRow)
            close_car_door(carRow)
            Mode6_GJB_DN_Empty_in_One_2(carPLC, carRow)
            return_origin(carRow)
        End If
    End Sub

    '只装法兰板：1车2个法兰板
    Private Sub mode3_Car1_action()

        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer
        If select_Car(carPLC, carRow) Then
            If carRow = 0 Then
                Mode3_FLB_UP_Full_in_One_1(carPLC, carRow)
                close_car_door(carRow)
                Mode3_FLB_DN_Empty_in_Two_2(carPLC, carRow)
                Mode3_FLB_UP_Empty_in_Two_3(carPLC, carRow)
                close_car_door(carRow)
                Mode3_FLB_DN_Empty_in_One_4(carPLC, carRow)
                return_origin(carRow)
            Else
                Mode3_FLB_UP_Empty_in_Two_3(carPLC, carRow)
                close_car_door(carRow)
                Mode3_FLB_DN_Empty_in_One_4(carPLC, carRow)
                Mode3_FLB_UP_Full_in_One_1(carPLC, carRow)
                close_car_door(carRow)
                Mode3_FLB_DN_Empty_in_Two_2(carPLC, carRow)
                return_origin(carRow)
            End If

        End If

    End Sub
    '只装骨架板:1车4个骨架板
    Private Sub Mode3_Car2_action()

        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer

        If select_Car(carPLC, carRow) Then
            If carRow = 0 Then
                Mode3_GJB_UP_Full_in_One_3(carPLC, carRow)
                close_car_door(carRow)
                Mode3_GJB_DN_Full_in_Two_4(carPLC, carRow)
                Mode3_GJB_UP_Empty_in_Two_1(carPLC, carRow)
                close_car_door(carRow)
                Mode3_GJB_DN_Empty_in_One_2(carPLC, carRow)
                return_origin(carRow)
            Else
                Mode3_GJB_UP_Empty_in_Two_1(carPLC, carRow)
                close_car_door(carRow)
                Mode3_GJB_DN_Empty_in_One_2(carPLC, carRow)
                Mode3_GJB_UP_Full_in_One_3(carPLC, carRow)
                close_car_door(carRow)
                Mode3_GJB_DN_Full_in_Two_4(carPLC, carRow)
                return_origin(carRow)
            End If

        End If

    End Sub

    ' 1车模式：车在1车间时
    Private Sub one_Car_Model_in_One(carPLC As SiemensS7Net, rowNo As Integer)
        Full_FLB_to_RGV_in_one_3(carPLC, rowNo)
        Full_GJB_to_RGV_in_one_5(carPLC, rowNo)
        close_car_door(rowNo)
        Full_to_JBGW_in_two_6(carPLC, rowNo)
        If rowNo = 0 Then
            Empty_to_RGV_in_two_1(carPLC, rowNo)
            close_car_door(rowNo)
            Empty_FLB_to_JBGW_in_one_2(carPLC, rowNo)
            Empty_GJB_to_JBGW_in_one_4(carPLC, rowNo)
            return_origin(rowNo)
        End If
    End Sub

    '1车模式：车在2车间时
    Private Sub one_Car_Model_in_Two(carPLC As SiemensS7Net, rowNo As Integer)
        Empty_to_RGV_in_two_1(carPLC, rowNo)
        close_car_door(rowNo)
        Empty_FLB_to_JBGW_in_one_2(carPLC, rowNo)
        Full_FLB_to_RGV_in_one_3(carPLC, rowNo)
        Empty_GJB_to_JBGW_in_one_4(carPLC, rowNo)
        Full_GJB_to_RGV_in_one_5(carPLC, rowNo)
        close_car_door(rowNo)
        Full_to_JBGW_in_two_6(carPLC, rowNo)
        return_origin(rowNo)

    End Sub
    Private Sub th_car2_method()
        one_Car_Model_in_Two(plc_car2, 1)
    End Sub

#End Region

    Dim cliSocket As Socket

    Dim FLB_Reader_Thread As Thread
    Dim intReaderNum As Integer = 0
    Dim FLB_Reader_Begin As Boolean = False
    Private Sub read_FLB_BOX_Num()
        If FLB_Reader_Begin = False Then
            '如果法兰板前工位数量为0，并且 料框数 > 0 则扫描料框
            If toInt(dtJBGWName.Rows(_1FMS_ROW)("gw1Sum")) = 0 And toInt(dtJBGWName.Rows(_1FMS_ROW)("gwLKNum")) > 0 Then


                Dim remoteEP As New IPEndPoint(Net.IPAddress.Parse(_1FMS_READER_IP), 2001)
                cliSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                Try
                    cliSocket.Connect(remoteEP)
                    writeInfoLog("1FMS读码器Socket打开")

                    'send "start" to Reader
                    Dim msg As Byte() = Encoding.UTF8.GetBytes("start")
                    Dim bytesSent As Integer = cliSocket.Send(msg)
                    writeInfoLog("Send to 1FMS读码器 【Start】:0")

                    FLB_Reader_Begin = True

                    '开始监听法兰板号
                    FLB_Reader_Thread = New Thread(AddressOf FLB_Reader_ReciveMsg)
                    FLB_Reader_Thread.Start()

                Catch ex As Exception
                    writeInfoLog("法兰板满框上料处读码器连接失败")
                End Try
            End If
        End If
    End Sub

    Private Sub FLB_Reader_ReciveMsg()
        Dim bytes() As Byte = New Byte(1024) {}
        Dim data As String = String.Empty

        While True
            Dim bytesRec As Integer = cliSocket.Receive(bytes)
            data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
            writeInfoLog("1FMS读码器内容：" & data)
            If data.Equals("<p>NoRead</p>") = False Then
                Dim receiveDataStrReplace = data.Replace("<p>", "").Replace("</p>", "")

                '读取法兰板里的片数
                Dim flbNum As Integer = getFLB_LK_num(receiveDataStrReplace)
                If flbNum > 0 Then
                    '更新1FMS的前工位片数
                    If plc_1_FLB.Write("DB1.DBW10", flbNum).IsSuccess Then
                        dtJBGWName.Rows(_1FMS_ROW)("gw1Sum") = flbNum
                        writeInfoLog("1FMS前工位片数：" & flbNum)
                    Else
                        writeInfoLog("1FMS前工位片数 写入失败")
                    End If
                End If
                intReaderNum = 0
                '关闭Sockect
                cliSocket.Shutdown(SocketShutdown.Both)
                cliSocket.Close()
                writeInfoLog("1FMS读码器Socket关闭")
                FLB_Reader_Begin = False
                Exit Sub
            Else
                If intReaderNum < 10 Then
                    intReaderNum += 1
                    writeInfoLog("Send to 1FMS读码器 【Start】：" & intReaderNum)
                    Dim msg As Byte() = Encoding.UTF8.GetBytes("start")
                    Dim bytesSent As Integer = cliSocket.Send(msg)
                Else
                    FLB_Reader_Begin = False
                End If
            End If
        End While
    End Sub

    Private Function getFLB_LK_num(_dl_no As String) As Integer

        Dim szSql As String = ""

        szSql &= "select * from  sf.b_flb_info where dl_no=? and status<>'2' "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, _dl_no)
        If IsNothing(dt) Then
            Return 0
        Else
            If dt.Rows.Count = 0 Then
                Return 0
            Else
                Return toInt(dt.Rows(0)("piece_num"))
            End If
        End If
    End Function

#Region "Other"
    Public Sub connectDB()
        'イニシャルファイル情報取得

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        '得到当前用户名
        g_LoginUser = GetIniFile("LOGIN", "user", "admin", g_szInitFileName)

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

        setAllErrMsg()
    End Sub
    Public Function getRGVModel() As Integer

        Dim _rgvModel As String = getSysParam("RGV_model")
        If _rgvModel <> "" Then
            If _rgvModel = "1" Then
                lblRgvModel.Text = "单车模式"
            ElseIf _rgvModel = "2" Then
                lblRgvModel.Text = "双车模式"
            End If
        Else
            _rgvModel = "0"
        End If
        getRGVModel = CInt(_rgvModel)


    End Function
    Private Function toInt(_item As Object) As Integer
        Return CInt(DBNULLtoZero(_item))
    End Function

    Private Sub addRow(_text As String)
        Dim _dr As DataRow
        _dr = dtJBGWName.NewRow()
        _dr("gwName") = _text
        dtJBGWName.Rows.Add(_dr)
        dtJBGWName.AcceptChanges()
    End Sub

    Private Sub frmRGV_JBGW_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Me.Hide()
        e.Cancel = True
    End Sub

    ''' <summary>
    ''' 更新系统参数表里的骨架板在线数量
    ''' </summary>
    ''' <param name="drTask">数据行</param>
    Private Sub setGJB_Num_in_YZZ(drTask As DataRow)
        Dim act() As String = drTask("active").ToString.Split(":")
        Dim _rollerNo As String = act(1).Substring(0, 2) '滚轮号码 如：1#/2#
        Dim _fbFLG As String = act(1).Substring(2, 3)    '法骨分类 如：GJB/FLB
        Dim _UpDnFLG As String = act(1).Substring(5, 2)  '上 下 料 如：Up/Dn
        Dim _JBGB_ROW_No As Integer = CInt(act(2))

        Dim szSql As String = ""
        If _fbFLG = "GJB" And _JBGB_ROW_No = _2FMX_ROW And _JBGB_ROW_No = _2FKS_ROW And _JBGB_ROW_No = _2GMX_ROW And _JBGB_ROW_No = _2GKS_ROW Then
            szSql &= "update  sf.t_sys_param "
            If _UpDnFLG = "DN" Then
                szSql &= "  set param_value=cast(param_value as numeric) + 1 "
            Else
                szSql &= "  set param_value=cast(param_value as numeric) - 1 "
            End If
            szSql &= "  WHERE param_key ='YZZ_GJB_BOX_Num' "

            If DBacs.ExecuteUpdateSql(szSql, -1) <> -2 Then
                If _UpDnFLG = "DN" Then
                    'nudGJBnum.Value = nudGJBnum.Value + 1
                    lblsysGJBnum.Text = CInt(lblsysGJBnum.Text) + 1
                    setSysParam("YZZ_GJB_BOX_Num", lblsysGJBnum.Text)
                Else
                    'nudGJBnum.Value = nudGJBnum.Value - 1
                    lblsysGJBnum.Text = CInt(lblsysGJBnum.Text) - 1
                    setSysParam("YZZ_GJB_BOX_Num", lblsysGJBnum.Text)
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>

    Private Function getFLB_1FMS_Reader_IP() As String
        Dim szSql As String = ""
        szSql &= "SELECT * FROM sf.t_hw_setting "
        szSql &= " where equip_id = 'Scaner3' and workshop_id = '1' "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        If IsNothing(dt) Then
            getFLB_1FMS_Reader_IP = 1
        Else
            If dt.Rows.Count > 0 Then
                getFLB_1FMS_Reader_IP = dt.Rows(0)("ip")
            Else
                getFLB_1FMS_Reader_IP = "127.0.0.1"
            End If
        End If
    End Function

    Private Sub writeInfoLog(logText As String)
        '写DataTable
        'Dim _newRow As DataRow = dtLogName.NewRow()
        '_newRow("dtLogNo") = dtLogName.Rows.Count + 1
        '_newRow("logTime") = Now
        '_newRow("logText") = logText
        'dtLogName.Rows.Add(_newRow)
        Try
            txtMemo.Text = Now & " : 【Info】  " & logText & vbCrLf & txtMemo.Text
            '写log
            logfile.WriteInfo(logText)
        Catch ex As Exception

        End Try

    End Sub
    Private Sub writeErrorLog(logText As String)
        '写DataTable
        'Dim _newRow As DataRow = dtLogName.NewRow()
        '_newRow("dtLogNo") = dtLogName.Rows.Count + 1
        '_newRow("logTime") = Now
        '_newRow("logText") = logText
        'dtLogName.Rows.Add(_newRow)

        txtMemo.Text = Now & " : 【ERROR】  " & logText & vbCrLf & txtMemo.Text

        '写log
        logfile.WriteError(logText)
    End Sub
    Private Sub tsslblLog_Click(sender As Object, e As EventArgs) Handles tsslblLog.Click
        Dim logForm As New FormLogNetView
        logForm.Show()
    End Sub
    Private Function getYZZFinishCount() As Integer
        Dim strSQL As String = ""

        strSQL = "select plan_quantity, b_pgd_sys_id from sf.b_workshop_current where factory_id=? and workshop=? and current_section=?"

        Dim dt1 As DataTable = DBacs.ExecuteSql(strSQL, -1, g_factory_id, g_cj_yzz, "YZZ")
        If dt1.Rows.Count > 0 Then
            strSQL = "select count(*) as finishedCount from sf.b_prod_info where pgd_id =? and status<>'0'"
            Dim dt2 As DataTable = DBacs.ExecuteSql(strSQL, -1, dt1.Rows(0)("b_pgd_sys_id").ToString)
            If dt2.Rows.Count > 0 Then
                Return CInt(dt1.Rows(0)("plan_quantity")) - CInt(dt2.Rows(0)("finishedCount"))
            End If
        End If
        Return 0

    End Function
    Private Sub chkMonitor_CheckedChanged(sender As Object, e As EventArgs) Handles chkMonitor.CheckedChanged
        If chkMonitor.Checked = True Then

            GJB_TYPE_IS_EQUALS = Equals_GJB2_YZZ()
            Dim RGV_model As Integer = getRGVModel()

            'setSysParam("GJB_need_Count_For_YZZ", getYZZFinishCount())'****************************************************
            NudGJBNeed.Value = getSysParam("GJB_need_Count_For_YZZ")
            lblsysGJBNeed.Text = NudGJBNeed.Value

            nudFLBNeed.Value = getSysParam("FLB_need_Count_For_YZZ")
            lblsysFLBNeed.Text = nudFLBNeed.Value

            nudGJBnum.Value = getSysParam("YZZ_GJB_BOX_Num")
            lblsysGJBnum.Text = nudGJBnum.Value
            _Server_Run = True
            '车1
            'th_car1 = New Threading.Thread(AddressOf readCar1)
            'th_car1.Start()
            ''车2
            'th_car2 = New Threading.Thread(AddressOf readCar2)
            'th_car2.Start()
            ''接驳工位
            'th_JBGW = New Threading.Thread(AddressOf Read_JBGW_PLC)
            'th_JBGW.Start()
            ''主线程
            'th_Main = New Threading.Thread(AddressOf MainThread)
            'th_Main.Start()
            tm.Interval = 500
            tm.Start()

            th_JBGW_RGV = New Thread(AddressOf getJBGW_RGV_DATA)
            th_JBGW_RGV.Start()
        Else
            tm.Stop()

            lblPlan.Text = "计划："
            _Server_Run = False
        End If
    End Sub

    Private Sub chkActive_CheckedChanged(sender As Object, e As EventArgs) Handles chkActive.CheckedChanged
        If chkActive.Checked = True Then
            _Active_Run = True

            '主线程
            th_Main = New Threading.Thread(AddressOf MainThread)
            th_Main.Start()
        Else
            _Active_Run = False
        End If
    End Sub

    Private Sub tmHMI_Tick(sender As Object, e As EventArgs) Handles tmHMI.Tick
        Dim a As String = ""
        With dtJBGWName
            a = ""
            If _Server_Run Then
                Try
                    Dim _car1_Loc As String = DBNULLtoZero(dtCarName.Rows(0)("CurrentLoc"))
                    Dim _car2_Loc As String = DBNULLtoZero(dtCarName.Rows(1)("CurrentLoc"))

                    gw1FKX.setStatus(.Rows(_1FKX_ROW), _car1_Loc, _car2_Loc)
                    a &= "1:"
                    Thread.Sleep(100)
                    gw1FMS.setStatus(.Rows(_1FMS_ROW), _car1_Loc, _car2_Loc)
                    a &= "2:"
                    Thread.Sleep(100)
                    gw1GMS1_1.setStatus(.Rows(_1GMS1_1_ROW), _car1_Loc, _car2_Loc)
                    a &= "3:"
                    Thread.Sleep(100)
                    gw1GKX_1.setStatus(.Rows(_1GKX_1_ROW), _car1_Loc, _car2_Loc)
                    a &= "4:"
                    Thread.Sleep(100)
                    gw1GMS1_2.setStatus(.Rows(_1GMS1_2_ROW), _car1_Loc, _car2_Loc)
                    a &= "5:"
                    Thread.Sleep(100)
                    gw1GKX_3.setStatus(.Rows(_1GKX_3_ROW), _car1_Loc, _car2_Loc)
                    a &= "6:"
                    Thread.Sleep(100)
                    gw1GMS_3.setStatus(.Rows(_1GMS_3_ROW), _car1_Loc, _car2_Loc)

                    Thread.Sleep(100)
                    gw2FKS.setStatus(.Rows(_2FKS_ROW), _car1_Loc, _car2_Loc)
                    a &= "7:"
                    Thread.Sleep(100)
                    gw2FMX.setStatus(.Rows(_2FMX_ROW), _car1_Loc, _car2_Loc)
                    a &= "8:"
                    Thread.Sleep(100)
                    gw2GKS.setStatus(.Rows(_2GKS_ROW), _car1_Loc, _car2_Loc)
                    a &= "9:"
                    Thread.Sleep(100)
                    gw2GMX.setStatus(.Rows(_2GMX_ROW), _car1_Loc, _car2_Loc)
                    a &= "10:"
                    Thread.Sleep(100)
                    Rgv1.setStatus(dtCarName.Rows(0))
                    a &= "11:"
                    Thread.Sleep(100)
                    Rgv2.setStatus(dtCarName.Rows(1))
                    a &= "12:"
                    Thread.Sleep(100)
                Catch ex As Exception
                    writeInfoLog("6:" & ex.Message & "--" & a)
                End Try
            End If
        End With


    End Sub

    Private Sub btnGJBNumOnLine_Click(sender As Object, e As EventArgs) Handles btnGJBNumOnLine.Click

        setSysParam("YZZ_GJB_BOX_Num", nudGJBnum.Value)
        setSysParam("FLB_need_Count_For_YZZ", nudFLBNeed.Value)
        setSysParam("GJB_need_Count_For_YZZ", NudGJBNeed.Value)
        lblsysGJBNeed.Text = getSysParam("GJB_need_Count_For_YZZ")
        lblsysFLBNeed.Text = getSysParam("FLB_need_Count_For_YZZ")
        lblsysGJBnum.Text = getSysParam("YZZ_GJB_BOX_Num")

    End Sub

    Private Sub setJBGW_Data()
        Dim szSql As String = ""

        szSql = "select * from sf.b_record_jbgw_status_monitor order by sort asc "
        dtJBGWName = DBacs.ExecuteSql(szSql, -1)

    End Sub
    Private Sub setRGV_Data()
        Dim szSql As String = ""

        szSql = "select * from sf.b_record_rgv_status_monitor order by carname  "
        dtCarName = DBacs.ExecuteSql(szSql, -1)

    End Sub

    Private Function saveLog(_dr As DataRow)
        Dim szSql As String = ""
        szSql &= " INSERT INTO sf.b_record_log_monitor(logtext, logtime) VALUES (?, ?) "
        Return DBacs.ExecuteUpdateSql(szSql, -1, _dr("logtext"), _dr("logtime"))
    End Function

    Private Function newTaskDetail(_dr As DataRow) As Integer
        Dim szSql As String = ""
        Try
            szSql &= " INSERT INTO sf.b_rgv_task_detail( taskno , task_item_no , active , car , status , finished_dt) "
            szSql &= " VALUES ( ? , ? , ? , ? , ? , ?) "
            Return DBacs.ExecuteUpdateSql(szSql, -1, _dr("taskNo"), _dr("task_item_no"), _dr("active"), _dr("car"), _dr("status"), _dr("finished_dt"))
        Catch ex As Exception

        End Try


    End Function

    Private Function updateTaskDetail(_dr As DataRow) As Integer

        updateTask(_dr("status"))

        Dim szSql As String = ""
        szSql &= " UPDATE sf.b_rgv_task_detail  "
        szSql &= " SET "
        szSql &= "   status = ? "
        szSql &= "  , finished_dt = ?  "
        szSql &= "  WHERE "
        szSql &= "  taskno = ? "
        szSql &= "  and task_item_no = ? "

        Return DBacs.ExecuteUpdateSql(szSql, -1, _dr("status"), _dr("finished_DT"), taskNo, _dr("task_item_no"))

    End Function
    Private Function clearTask() As Integer

        Dim szSql As String = ""
        DBacs.BeginTransaction()
        Try
            szSql = " delete from sf.b_rgv_task_detail where taskno=? "
            DBacs.ExecuteUpdateSql(szSql, -1, taskNo)

            szSql = " delete from sf.b_rgv_task where taskno=? "
            DBacs.ExecuteUpdateSql(szSql, -1, taskNo)
            DBacs.CommitTransaction()
            Return 1
        Catch ex As Exception
            DBacs.RollbackTransaction()
            Return 2
        End Try

    End Function
    Private Function newTask(_memo As String) As Integer
        Dim szSql As String = ""
        szSql &= "INSERT INTO sf.b_rgv_task(taskno, status, memo,update_time) VALUES (?, ?, ?,?) "
        Return DBacs.ExecuteUpdateSql(szSql, -1, taskNo, CInt(TASK_STATUS.wait), _memo, Now)
    End Function

    Private Function updateTask(_status As TASK_STATUS) As Integer
        Dim szSql As String = ""
        szSql &= " UPDATE sf.b_rgv_task  "
        szSql &= " SET "
        szSql &= "   status = ? "
        szSql &= "  , update_time = ?  "
        szSql &= "  WHERE "
        szSql &= "  taskno = ? "

        Return DBacs.ExecuteUpdateSql(szSql, -1, CInt(_status), Now, taskNo)
    End Function

    Private Sub getTaskAndTaskDetail()
        Dim szSql As String = ""
        szSql &= "select * from sf.b_rgv_task where status<>?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, CInt(TASK_STATUS.AllCompleted))
        If dt.Rows.Count = 0 Then
            lblTaskNo.Text = ""
            lblMemo.Text = ""
            taskNo = ""
            'Dim _dt As DataTable
            If IsNothing(dgvTask.DataSource) = False Then
                dgvTask.DataSource.clear
            End If
            'Dim _dt As DataTable = dgvTask.DataSource
            '_dt.Clear()

            Actioning = False
        End If

    End Sub

    Private Sub setTaskAndTaskDetail()
        Dim szSql As String = ""
        szSql &= "select * from sf.b_rgv_task where status<>?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, CInt(TASK_STATUS.AllCompleted))
        If dt.Rows.Count > 0 Then
            lblTaskNo.Text = dt.Rows(0)("taskno")
            lblMemo.Text = dt.Rows(0)("memo")
            taskNo = dt.Rows(0)("taskno")

            szSql = "select * from sf.b_rgv_task_detail where taskno=? order by task_item_no asc"
            dtTask = DBacs.ExecuteSql(szSql, -1, dt.Rows(0)("taskno"))
            dgvTask.DataSource = dtTask
        Else
            dgvTask.DataSource = Nothing
        End If
    End Sub

    ''' <summary>
    ''' 当dataGridview遇到问题时，运行到此方法。（不用进行处理）
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub DGV_JBGW_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles dgvTask.DataError
        ' 当dataGridview遇到问题时，运行到此方法。（不用进行处理）
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(Math.Ceiling(12.1))
        MsgBox(Math.Ceiling(13.6))
        'Dim txt As String = "1#GJBDN"

        'Dim _rollerNo As String = txt.Substring(0, 2) '滚轮号码 如：1#/2#
        'Dim _fbFLG As String = txt.Substring(2, 3)    '法骨分类 如：GJB/FLB
        'Dim _UpDnFLG As String = txt.Substring(5, 2)  '上 下 料 如：Up/Dn
        'MsgBox(_rollerNo)
        'MsgBox(_fbFLG)
        'MsgBox(_UpDnFLG)
        ''testPLC控件----测用
        'Dim testPLC As SiemensS7Net
        'testPLC = New SiemensS7Net(SiemensPLCS.S1200)
        'With testPLC

        '    .IpAddress = "127.0.0.1"
        '    .Port = 102
        '    .ConnectServer()
        'End With
        'Button1.Text = testPLC.ReadBool("DB16.1.0").Content.ToString

        ''A条件
        'Dim _arrivedTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                             Return testPLC.WaitAsync("DB16.1.0", True, 500, -1)
        '                                                                         End Function)
        ''B条件
        'Dim _remoteStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                                Return testPLC.WaitAsync("DB16.1.1", True, 500, -1)
        '                                                                            End Function)
        ''C条件
        'Dim _carStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                             Return testPLC.WaitAsync("DB16.1.2", True, 500, -1)
        '                                                                         End Function)
        ''****任务数组****



        'Dim tasks As Task() = {_arrivedTaskResult, _remoteStopTaskResult, _carStopTaskResult}

        'Dim _CompletedIndex As Integer = Task.WaitAny(tasks, 10 * 1000)
        'Button1.Text = _CompletedIndex
        'If _CompletedIndex = 1 Then 'A条件满足
        '    If _arrivedTaskResult.Result.IsSuccess Then '正常完了
        '        writeInfoLog(String.Format("A条件满足"))
        '    Else
        '        writeErrorLog(String.Format("A条件异常：{0}:{1}", _arrivedTaskResult.Result.ErrorCode, _arrivedTaskResult.Result.Message))
        '    End If
        'End If

        'If _CompletedIndex = 2 Then '远程急停
        '    If _remoteStopTaskResult.Result.IsSuccess Then
        '        writeErrorLog(String.Format("B条件满足"))
        '    End If
        'End If

        'If _CompletedIndex = 3 Then '本地急停
        '    If _remoteStopTaskResult.Result.IsSuccess Then
        '        writeErrorLog(String.Format("C条件满足"))
        '    End If
        'End If
    End Sub

    Private Function Equals_GJB2_YZZ() As Boolean
        Dim strSQL As String = ""

        strSQL &= " select b.gjb_material_code material_code  ,c.mst_value gjb_type"
        strSQL &= " from sf.b_workshop_current a inner join sf.t_prod_spec b "
        strSQL &= " On a.support_model=b.prod_spec_id "
        strSQL &= " inner join sf.t_mst_gjb c on c.material_code=b.gjb_material_code  "
        strSQL &= "where a.workshop=? and a.current_section=?"
        Dim dtGJBZP As DataTable = DBacs.ExecuteSql(strSQL, -1, g_cj_pt, g_ws_pt_gjbzp)

        If dtGJBZP.Rows.Count > 0 AndAlso DBNULLtoBlank(dtGJBZP.Rows(0)("material_code")) <> "" Then
        Else
            lblPlan.Text = "辊涂计划内容不全"
            Return False
        End If

        strSQL = ""
        strSQL &= " select b.gjb_material_code material_code ,c.mst_value gjb_type"
        strSQL &= " from sf.b_workshop_current a inner join sf.t_prod_spec b "
        strSQL &= " On a.support_model=b.prod_spec_id "
        strSQL &= " inner join sf.t_mst_gjb c on c.material_code=b.gjb_material_code  "
        strSQL &= " where  a.workshop=? and current_section=?"
        Dim dtYZZ As DataTable = DBacs.ExecuteSql(strSQL, -1, g_cj_yzz, "YZZ")
        If dtYZZ.Rows.Count > 0 AndAlso DBNULLtoBlank(dtYZZ.Rows(0)("material_code")) <> "" Then
        Else
            lblPlan.Text = "预组装计划内容不全"
            Return False
        End If

        If dtGJBZP.Rows(0)("material_code") = dtYZZ.Rows(0)("material_code") Then
            lblPlan.Text = String.Format("计划相同：一车间：{0}({1}) 二车间：{2}({3}) ", dtGJBZP.Rows(0)("gjb_type"), dtGJBZP.Rows(0)("material_code"), dtYZZ.Rows(0)("gjb_type"), dtYZZ.Rows(0)("material_code"))
            GJB_BOX_TYPE_EQUALS = True '判断料框是否可通用
            lblPlan.ForeColor = Color.DarkGreen
            Return True
        Else
            '判断料框是否可通用
            If dtGJBZP.Rows(0)("gjb_type").ToString.Contains("1000") Or dtGJBZP.Rows(0)("gjb_type").ToString.Contains("900") Then
                If dtYZZ.Rows(0)("gjb_type").ToString.Contains("1000") Or dtYZZ.Rows(0)("gjb_type").ToString.Contains("900") Then
                    GJB_BOX_TYPE_EQUALS = True '判断料框是否可通用
                Else
                    GJB_BOX_TYPE_EQUALS = False '判断料框是否可通用
                End If
            End If
            If dtGJBZP.Rows(0)("gjb_type").ToString.Contains("700") Or dtGJBZP.Rows(0)("gjb_type").ToString.Contains("800") Then
                If dtYZZ.Rows(0)("gjb_type").ToString.Contains("700") Or dtYZZ.Rows(0)("gjb_type").ToString.Contains("800") Then
                    GJB_BOX_TYPE_EQUALS = True '判断料框是否可通用
                Else
                    GJB_BOX_TYPE_EQUALS = False '判断料框是否可通用
                End If
            End If
            If dtGJBZP.Rows(0)("gjb_type").ToString.Contains("500") Or dtGJBZP.Rows(0)("gjb_type").ToString.Contains("600") Then
                If dtYZZ.Rows(0)("gjb_type").ToString.Contains("500") Or dtYZZ.Rows(0)("gjb_type").ToString.Contains("600") Then
                    GJB_BOX_TYPE_EQUALS = True '判断料框是否可通用
                Else
                    GJB_BOX_TYPE_EQUALS = False '判断料框是否可通用
                End If
            End If
            lblPlan.Text = String.Format("计划不同：一车间：{0}({1}) 二车间：{2}({3}) ", dtGJBZP.Rows(0)("gjb_type"), dtGJBZP.Rows(0)("material_code"), dtYZZ.Rows(0)("gjb_type"), dtYZZ.Rows(0)("material_code"))
            lblPlan.ForeColor = Color.Red
            Return False
        End If

    End Function

#End Region

#Region "动作的详细方法"
    '选车
    Private Function select_Car(ByRef _carPLC As SiemensS7Net, ByRef _carRow As Integer) As Boolean
        '选车------------------
        Dim drCar1 As DataRow = dtCarName.Rows(0)
        Dim drCar2 As DataRow = dtCarName.Rows(1)

        'Dim carPLC As SiemensS7Net
        'Dim carRow As Integer
        Dim car1Battery As Double = 0
        Dim car2Battery As Double = 0
        If DBNULLtoBlank(drCar1("Communication")).ToLower = "true" Then
            car1Battery = CDbl(DBNULLtoZero(drCar1("Battery")))
        End If
        If DBNULLtoBlank(drCar2("Communication")).ToLower = "true" Then
            car2Battery = CDbl(DBNULLtoZero(drCar2("Battery")))
        End If
        If car1Battery = 0 And car2Battery = 0 Then
            Return False
        End If

        Dim car1_chk As Boolean = CBool(getSysParam("RGV_AUTO_CAR").Split(",")(0))
        Dim car2_chk As Boolean = CBool(getSysParam("RGV_AUTO_CAR").Split(",")(1))
        If car1_chk = True And car2_chk = True Then


            '选车----------------
            If drCar1("in1") = "0" And drCar1("out1") = "0" And drCar1("in2") = "0" And drCar1("out2") = "0" And drCar2("in1") = "0" And drCar2("out1") = "0" And drCar2("in2") = "0" And drCar2("out2") = "0" Then
                If car1Battery > car2Battery Then
                    _carPLC = plc_car1
                    _carRow = 0
                    Return True
                Else
                    _carPLC = plc_car2
                    _carRow = 1
                    Return True
                End If

            ElseIf drCar1("in1") = "0" And drCar1("out1") = "0" And drCar1("in2") = "0" And drCar1("out2") = "0" Then
                _carPLC = plc_car1
                _carRow = 0
                Return True
            ElseIf drCar2("in1") = "0" And drCar2("out1") = "0" And drCar2("in2") = "0" And drCar2("out2") = "0" Then
                _carPLC = plc_car2
                _carRow = 1
                Return True
            End If

        End If

        If car1_chk = True And drCar1("in1") = "0" And drCar1("out1") = "0" And drCar1("in2") = "0" And drCar1("out2") = "0" Then
            _carPLC = plc_car1
            _carRow = 0
            Return True
        End If

        If car2_chk = True And drCar2("in1") = "0" And drCar2("out1") = "0" And drCar2("in2") = "0" And drCar2("out2") = "0" Then
            _carPLC = plc_car2
            _carRow = 1
            Return True
        End If
        Return False
    End Function
    Private Sub Empty_to_RGV_in_two_1(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        'Step1:上空(法，骨)
        Dim _1FKX_Empty_Count As Integer = 2 - dtJBGWName(_1FKX_ROW)("gwLKNum")
        '骨架板2框
        'RGV移动到2GKS=>最多上2个空框
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "UP"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_2 'plc_2_GJB
            ._JBGB_ROW_No = _2GKS_ROW
            ._location = "2#-2GKS"
            ._num = 1
        End With
        If toInt(dtJBGWName(_2GKS_ROW)("gwLKNum")) > 0 Then
            addTask(objActEn)
        End If

        '骨架板2框
        If toInt(dtJBGWName(_2GKS_ROW)("gwLKNum")) > 1 Then
            addTask(objActEn)
        End If
        '--------------------------------------------------
        '法兰板1框
        'RGV移动到2FKS=>1个空框
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "UP"
            ._fbFLG = "FLB"
            ._jbgwPLC = plc_2 'plc_2_FLB
            ._JBGB_ROW_No = _2FKS_ROW
            ._location = "1#-2FKS"
            ._num = 1
        End With
        If toInt(dtJBGWName(_2FKS_ROW)("gwLKNum")) >= 1 And _1FKX_Empty_Count > 0 Then
            addTask(objActEn)
        End If
        '---------------------------------------------------
    End Sub

    Private Sub Empty_FLB_to_JBGW_in_one_2(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        'Step2:下法空
        'RGV移动到1FKX => 下1个空框

        '法兰板1框
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "DN"
            ._fbFLG = "FLB"
            ._jbgwPLC = plc_1_FLB
            ._JBGB_ROW_No = _1FKX_ROW
            ._location = "1#-1FKX"
            ._num = 1
        End With

        '车上是否有法兰板
        Dim flbLKcount As Integer
        If rowNo = 0 Then
            flbLKcount = FLB_LK_count_in_Car1
        Else
            flbLKcount = FLB_LK_count_in_Car2
        End If
        If flbLKcount = 1 Then

            If 2 - dtJBGWName(_1FKX_ROW)("gwLKNum") >= 1 Then
                addTask(objActEn)
            Else
                '如果没有空位就要报警
            End If
        End If

    End Sub

    Private Sub Full_FLB_to_RGV_in_one_3(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        'Step3:上法满框
        'RGV移动到1FMS => 上1个满框
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "UP"
            ._fbFLG = "FLB"
            ._jbgwPLC = plc_1_FLB
            ._JBGB_ROW_No = _1FMS_ROW
            ._location = "1#-1FMS"
            ._num = 1
        End With
        'Dim _1FMS_MES_PLAN_Num As Integer = toInt(dtJBGWName(_1FMS_ROW)("mesPlanNum"))

        If toInt(dtJBGWName(_1FMS_ROW)("gwLKNum")) >= 1 And FLB_Need_Count Then ' _1FMS_MES_PLAN_Num > 0 Then
            addTask(objActEn)
        Else
            '*如没有满框，接驳工位要报警	
        End If

    End Sub

    Private Sub Empty_GJB_to_JBGW_in_one_4(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn2 As New activeEn
        Dim objActEn3 As New activeEn

        With objActEn2
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_1_GJB2
            ._JBGB_ROW_No = _1GKX_1_ROW
            ._location = "2#-1GKX-1"
            ._num = 1
        End With
        With objActEn3
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_1_GJB3
            ._JBGB_ROW_No = _1GKX_3_ROW
            ._location = "2#-1GKX-3"
            ._num = 1
        End With
        '    如果1GKX-1有2个空框位
        '        RGV移动到1GKX-1 >= 最多下2个空框   
        '    否则
        '        RGV移动到1GKX-3 >= 最多下2个空框   
        '		*如没有空框位，接驳工位要报警	    
        Dim Empty_GJB_Num_In_Car As Integer = 0

        If rowNo = 0 Then
            Empty_GJB_Num_In_Car = GJB_LK_count_in_Car1
        Else
            Empty_GJB_Num_In_Car = GJB_LK_count_in_Car2
        End If

        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And plc_1_GJB2_AutoMode = True Then

            Dim empty_num_in_2 As Integer = 4 - toInt(dtJBGWName(_1GKX_1_ROW)("gwLKNum"))
            Dim empty_num_in_3 As Integer = 4 - toInt(dtJBGWName(_1GKX_3_ROW)("gwLKNum"))
            '车上满框骨架板的数量 
            If empty_num_in_2 >= 2 Then
                '如果 1GKX-1有2个空，下2个空框 
                If Empty_GJB_Num_In_Car > 0 Then
                    addTask(objActEn2)
                    If Empty_GJB_Num_In_Car > 1 Then
                        addTask(objActEn2)
                    End If
                End If

            ElseIf empty_num_in_2 = 1 Then

                If Empty_GJB_Num_In_Car > 0 Then
                    '如果 1GKX-1有1个空，下1个空框 
                    addTask(objActEn2)
                    If Empty_GJB_Num_In_Car > 1 Then
                        '去1GKX3，下1个
                        addTask(objActEn3)
                    End If
                End If

            ElseIf empty_num_in_2 <= 0 Then
                '如果 1GKX-1没有空，去1GKX3下2个空框 
                If Empty_GJB_Num_In_Car > 0 Then
                    addTask(objActEn3)
                    If Empty_GJB_Num_In_Car > 1 Then
                        addTask(objActEn3)
                    End If
                End If

            End If
        Else
            If Empty_GJB_Num_In_Car > 0 Then
                addTask(objActEn3)
                If Empty_GJB_Num_In_Car > 1 Then
                    addTask(objActEn3)
                End If
            End If
        End If
        'GJB_TYP_1<>GJB_TYP_3			
        '	RGV移动到1GKX-3=>最多下2个空框		
        '	*如没有空框位，接驳工位要报警		

    End Sub

    Private Sub Full_GJB_to_RGV_in_one_5(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn2_1 As New activeEn
        Dim objActEn2_2 As New activeEn
        Dim objActEn3 As New activeEn
        'GJB_TYP_1 = GJB_TYP_3
        With objActEn2_1
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "UP"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_1_GJB2
            ._JBGB_ROW_No = _1GMS1_1_ROW
            ._location = "2#-1GMS1-1"
            ._num = 1
        End With
        With objActEn2_2
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "UP"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_1_GJB2
            ._JBGB_ROW_No = _1GMS1_2_ROW
            ._location = "2#-1GMS1-2"
            ._num = 1
        End With
        With objActEn3
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "UP"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_1_GJB3
            ._JBGB_ROW_No = _1GMS_3_ROW
            ._location = "2#-1GMS-3"
            ._num = 1
        End With
        '	GJB_TYP_1=GJB_TYP_3		
        If GJB_TYPE_IS_EQUALS And plc_1_GJB2_AutoMode = True Then

            '2#内框Up-----------------------
            Dim a As Integer = dtJBGWName(_1GMS1_1_ROW)("gwLKNum")
            Dim b As Integer = dtJBGWName(_1GMS1_2_ROW)("gwLKNum")
            'Dim _1GJB1_MES_PLAN_NUM As Integer =dtJBGWName(_1GMS1_1_ROW)("mesPlanNum")

            If GJB_Need_Count >= 2 Then
                If a > 0 Then
                    addTask(objActEn2_1)
                    If a > 1 And b = 0 Then
                        addTask(objActEn2_1)
                    End If
                End If
                If b > 0 Then
                    addTask(objActEn2_2)
                    If b > 1 And a = 0 Then
                        addTask(objActEn2_2)
                    End If
                End If
            End If '计划>=2

            If GJB_Need_Count = 1 Then
                If a > 0 Then
                    addTask(objActEn2_1)
                Else
                    If b > 0 Then
                        addTask(objActEn2_2)
                    End If
                End If
            End If '计划=2
        Else
            '-------------------------------
            '如果经过上方，还有未装满的料框，就到第三处去上料
            'Dim _1GJB_3_MES_PLAN_NUM As Integer = dtJBGWName(_1GMS_3_ROW)("mesPlanNum")
            Dim C As Integer = dtJBGWName(_1GMS_3_ROW)("gwLKNum")
            'Dim emptyNum As Integer = 2 - toInt(dtCarName(rowNo)("in2")) + toInt(dtCarName(rowNo)("out2"))
            'For i = 1 To emptyNum
            '    '如果1GMS3 没有2箱怎么办

            '    addTask(objActEn3)
            'Next
            If GJB_Need_Count >= 2 Then
                If C >= 2 Then
                    addTask(objActEn3)
                    addTask(objActEn3)
                End If
                If C = 1 Then
                    addTask(objActEn3)
                End If
            End If

            If GJB_Need_Count = 1 Then
                If C >= 1 Then
                    addTask(objActEn3)
                End If
            End If
            '	GJB_TYP_1<>GJB_TYP_3			
            '	RGV移动到1GMS-3=>最多上2个满框		
            '	*如没不足2框，接驳工位要报警		
        End If
    End Sub

    Private Sub Full_to_JBGW_in_two_6(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEnFLB As New activeEn
        Dim objActEnGJB As New activeEn

        '车上满框骨架板的数量 
        Dim Full_GJB_Num_In_Car As Integer = 0
        If rowNo = 0 Then
            Full_GJB_Num_In_Car = GJB_LK_count_in_Car1
        Else
            Full_GJB_Num_In_Car = GJB_LK_count_in_Car2
        End If
        'IIf(dtCarName(rowNo)("in2").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("out2").ToString = "1", 1, 0)

        '车上满框法兰板的数量 
        Dim Full_FLB_Num_In_Car As Integer = 0
        If rowNo = 0 Then
            Full_FLB_Num_In_Car = FLB_LK_count_in_Car1
        Else
            Full_FLB_Num_In_Car = FLB_LK_count_in_Car2
        End If
        'If dtCarName(rowNo)("in1").ToString = "1" Or dtCarName(rowNo)("out1").ToString = "1" Then
        '    Full_FLB_Num_In_Car = 1
        'End If

        With objActEnFLB
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "DN"
            ._fbFLG = "FLB"
            ._jbgwPLC = plc_2 ' plc_2_FLB
            ._JBGB_ROW_No = _2FMX_ROW
            ._location = "1#-2FMX"
            ._num = 1
        End With
        With objActEnGJB
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_2 ' plc_2_GJB
            ._JBGB_ROW_No = _2GMX_ROW
            ._location = "2#-2GMX"
            ._num = 1
        End With
        'TODO:关车门移动到2车间
        'If carPLC.ReadBool("DB17.26.1").Content = True Then
        '    Dim _act As String = modWinApi.GetIniFile("active", "DoorDN", "", Me.g_szInitFileName)
        '    car_send(carPLC, _act, True)
        '    carPLC.Wait("DB17.26.2", True, 500, 20 * 1000)
        'End If

        'RGV移动到2FMX => 下1个满框
        If Full_FLB_Num_In_Car = 1 Then
            addTask(objActEnFLB)
        End If
        'RGV移动到2GMX => 下2个满框
        If Full_GJB_Num_In_Car > 0 Then
            addTask(objActEnGJB)
            If Full_GJB_Num_In_Car > 1 Then
                addTask(objActEnGJB)
            End If
        End If
    End Sub

    Private Sub return_origin(rowNo As Integer)

        '先前车门
        If dtTask.Rows.Count > 1 Then
            If dtTask.Rows(dtTask.Rows.Count - 1)("active") <> "DO:DoorDN:0" Then
                close_car_door(rowNo)
            End If
        End If
        If dtTask.Rows.Count > 0 Then
            '回到原点
            Dim objCDW As New activeEn
            objCDW._RGV_ROW_No = rowNo
            objCDW._location = "CDW"
            addTask(objCDW)
        End If


    End Sub
    Private Sub close_car_door(carRowNo As Integer)
        If dtTask.Rows.Count > 0 Then
            Dim objen As New activeEn
            objen._RGV_ROW_No = carRowNo
            objen._UpDnFLG = "DoorDN"
            addTask(objen)
        End If

    End Sub

#Region "模式3-B：Car2"
    ''' <summary>
    ''' 模式3-B：第1步，骨架板空框上车2
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_GJB_UP_Empty_in_Two_1(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        '当2GKS的数量>0 
        Dim GJB_Empty_Num As Integer = DBNULLtoZero(dtJBGWName(_2GKS_ROW)("gwLKNum"))
        Dim _1CJempty_num As Integer = 0
        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And gw1GMS1_2.controlmode Then
            'If 4 - DBNULLtoZero(dtJBGWName(_1GKX_1_ROW)("gwLKNum")) >= GJB_Empty_Num Then
            'Else
            '    If 4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum")) >= GJB_Empty_Num Then
            '    Else
            '        Exit Sub
            '    End If
            'End If
            _1CJempty_num = (4 - DBNULLtoZero(dtJBGWName(_1GKX_1_ROW)("gwLKNum"))) + (4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum")))
        Else
            'If 4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum")) >= GJB_Empty_Num Then
            _1CJempty_num = 4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum"))
            'Else
            '    _1CJempty_num = 4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum"))
            '    'Exit Sub
            'End If
        End If

        If CInt(getSysParam("YZZ_GJB_BOX_Num")) > 0 And CInt(getSysParam("YZZ_GJB_BOX_Num")) < _1CJempty_num Then
            _1CJempty_num = getSysParam("YZZ_GJB_BOX_Num")
        End If
        If _1CJempty_num > 0 Then

            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "UP"
                ._fbFLG = "GJB"
                ._jbgwPLC = plc_2 ' plc_2_GJB
                ._JBGB_ROW_No = _2GKS_ROW
                ._location = "1#-2GKS"
                ._num = 1
            End With
            If _1CJempty_num >= 1 And GJB_Empty_Num >= 1 Then
                addTask(objActEn)
            End If
            If _1CJempty_num >= 2 And GJB_Empty_Num >= 2 Then
                '骨架板2框
                addTask(objActEn)
            End If

            If _1CJempty_num >= 3 And GJB_Empty_Num >= 3 Then
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "UP"
                    ._fbFLG = "GJB"
                    ._jbgwPLC = plc_2 ' plc_2_GJB
                    ._JBGB_ROW_No = _2GKS_ROW
                    ._location = "2#-2GKS"
                    ._num = 1
                End With
                '骨架板3框
                addTask(objActEn)
                If GJB_Empty_Num >= 4 Then
                    '骨架板4框
                    addTask(objActEn)
                End If
            End If

        End If
    End Sub

    ''' <summary>
    ''' 模式3-B:第2步，把骨架板空框放到一车间
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_GJB_DN_Empty_in_One_2(carPLC As SiemensS7Net, rowNo As Integer)
        '取得车上空框的数量
        Dim Empty_Num_In_Car2 As Integer = 0
        'IIf(dtCarName(rowNo)("in1").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("in2").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("out1").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("out2").ToString = "1", 1, 0)
        If rowNo = 0 Then
            Empty_Num_In_Car2 = GJB_LK_count_in_Car1
        Else
            Empty_Num_In_Car2 = GJB_LK_count_in_Car2
        End If

        Dim empty_num_1GKX_1 As Integer = 4 - DBNULLtoZero(dtJBGWName(_1GKX_1_ROW)("gwLKNum"))
        Dim empty_num_1GKX_3 As Integer = 4 - DBNULLtoZero(dtJBGWName(_1GKX_3_ROW)("gwLKNum"))
        Dim objActEn As New activeEn

        '当GJP_TYP_1=GJB_TYP_3 
        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And gw1GMS1_2.controlmode Then ' plc_1_GJB2_AutoMode = True Then
            '当1GKX-1的空框位数>=Car2上的框数时								
            'Car2移动到1GKX-1=>最多下4空框							
            If Empty_Num_In_Car2 <= empty_num_1GKX_1 Then

#Region "Car2的空框全放在1GKX-1"
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "1#"
                    ._UpDnFLG = "DN"
                    ._fbFLG = "GJB"
                    ._jbgwPLC = plc_1_GJB2
                    ._JBGB_ROW_No = _1GKX_1_ROW
                    ._location = "1#-1GKX-1"
                    ._num = 1
                End With

                If Empty_Num_In_Car2 >= 1 Then
                    addTask(objActEn)
                End If

                If Empty_Num_In_Car2 >= 2 Then
                    addTask(objActEn)
                End If

                If Empty_Num_In_Car2 >= 3 Then
                    With objActEn
                        ._RGV_ROW_No = rowNo
                        ._RGV_PLC = carPLC
                        ._rollerNo = "2#"
                        ._UpDnFLG = "DN"
                        ._fbFLG = "GJB"
                        ._jbgwPLC = plc_1_GJB2
                        ._JBGB_ROW_No = _1GKX_1_ROW
                        ._location = "2#-1GKX-1"
                        ._num = 1
                    End With
                    addTask(objActEn)
                    If Empty_Num_In_Car2 >= 4 Then
                        addTask(objActEn)
                    End If
                End If
#End Region

            Else

                '数组内表示4个怎么放
                Dim Car2BoxLoc(3) As Integer

                Car2BoxLoc(0) = 3 'in1
                Car2BoxLoc(1) = 3 'out1
                Car2BoxLoc(2) = 3 'in2
                Car2BoxLoc(3) = 3 'out2

                For i As Integer = 0 To empty_num_1GKX_1 - 1
                    Car2BoxLoc(i) = 1
                Next
                For i As Integer = 0 To Empty_Num_In_Car2 - 1
                    Dim _rollerNo As String = ""
                    Dim _location3 As String = "-1GKX-3"
                    Dim _location1 As String = "-1GKX-1"
                    Select Case i
                        Case 0, 1
                            _rollerNo = "1#"

                        Case 2, 3
                            _rollerNo = "2#"
                    End Select
                    _location3 = _rollerNo & _location3
                    _location1 = _rollerNo & _location1
                    If Car2BoxLoc(i) = 3 Then
                        With objActEn
                            ._RGV_ROW_No = rowNo
                            ._RGV_PLC = carPLC
                            ._rollerNo = _rollerNo
                            ._UpDnFLG = "DN"
                            ._fbFLG = "GJB"
                            ._jbgwPLC = plc_1_GJB3
                            ._JBGB_ROW_No = _1GKX_3_ROW
                            ._location = _location3
                            ._num = 1
                        End With

                        addTask(objActEn)
                        'Else
                        '    With objActEn
                        '        ._RGV_ROW_No = rowNo
                        '        ._RGV_PLC = carPLC
                        '        ._rollerNo = _rollerNo
                        '        ._UpDnFLG = "DN"
                        '        ._fbFLG = "GJB"
                        '        ._jbgwPLC = plc_1_GJB2
                        '        ._JBGB_ROW_No = _1GKX_1_ROW
                        '        ._location = _location1
                        '        ._num = 1
                        '    End With

                        '    addTask(objActEn)
                    End If
                Next
                For i As Integer = 0 To Empty_Num_In_Car2 - 1
                    Dim _rollerNo As String = ""
                    Dim _location3 As String = "-1GKX-3"
                    Dim _location1 As String = "-1GKX-1"
                    Select Case i
                        Case 0, 1
                            _rollerNo = "1#"

                        Case 2, 3
                            _rollerNo = "2#"
                    End Select
                    _location3 = _rollerNo & _location3
                    _location1 = _rollerNo & _location1
                    If Car2BoxLoc(i) = 3 Then
                        'With objActEn
                        '    ._RGV_ROW_No = rowNo
                        '    ._RGV_PLC = carPLC
                        '    ._rollerNo = _rollerNo
                        '    ._UpDnFLG = "DN"
                        '    ._fbFLG = "GJB"
                        '    ._jbgwPLC = plc_1_GJB3
                        '    ._JBGB_ROW_No = _1GKX_3_ROW
                        '    ._location = _location3
                        '    ._num = 1
                        'End With

                        'addTask(objActEn)
                    Else
                        With objActEn
                            ._RGV_ROW_No = rowNo
                            ._RGV_PLC = carPLC
                            ._rollerNo = _rollerNo
                            ._UpDnFLG = "DN"
                            ._fbFLG = "GJB"
                            ._jbgwPLC = plc_1_GJB2
                            ._JBGB_ROW_No = _1GKX_1_ROW
                            ._location = _location1
                            ._num = 1
                        End With

                        addTask(objActEn)
                    End If
                Next
            End If
        Else '当GJP_TYP_1 <> GJB_TYP_3 
#Region "Car2的空框全放在1GKX-3"
            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "DN"
                ._fbFLG = "GJB"
                ._jbgwPLC = plc_1_GJB3
                ._JBGB_ROW_No = _1GKX_3_ROW
                ._location = "1#-1GKX-3"
                ._num = 1
            End With
            If Empty_Num_In_Car2 >= 1 Then
                addTask(objActEn)
            End If
            If Empty_Num_In_Car2 >= 2 Then
                addTask(objActEn)
            End If

            If Empty_Num_In_Car2 >= 3 Then
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "DN"
                    ._fbFLG = "GJB"
                    ._jbgwPLC = plc_1_GJB3
                    ._JBGB_ROW_No = _1GKX_3_ROW
                    ._location = "2#-1GKX-3"
                    ._num = 1
                End With
                addTask(objActEn)
                If Empty_Num_In_Car2 >= 4 Then
                    addTask(objActEn)
                End If
            End If
#End Region
        End If

    End Sub

    ''' <summary>
    ''' 模式3-B：第3步，把满框的骨架板放到RGV车上
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_GJB_UP_Full_in_One_3(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        'Dim _1GJB_MES_PLAN_NUM As Integer =  dtJBGWName(_1GMS1_1_ROW)("mesPlanNum")
        Dim LKNum_1GM1_3 As Integer = dtJBGWName(_1GMS_3_ROW)("gwLKNum")

        If GJB_TYPE_IS_EQUALS Then
            '当GJP_TYP_1=GJB_TYP_3时

            '数组内表示4个怎么放
            Dim Car2BoxLoc(3) As Integer

            Car2BoxLoc(0) = 3 'out1
            Car2BoxLoc(1) = 3 'in1
            Car2BoxLoc(2) = 3 'out2
            Car2BoxLoc(3) = 3 'in2

            Dim LKNum_1GMS1_1 As Integer = dtJBGWName(_1GMS1_1_ROW)("gwLKNum")
            Dim LKNum_1GMS1_2 As Integer = dtJBGWName(_1GMS1_2_ROW)("gwLKNum")

            Dim sumLKnum = LKNum_1GM1_3 + LKNum_1GMS1_1 + LKNum_1GMS1_2

            '分配 每框从哪里取
            'If LKNum_1GMS1_1 + LKNum_1GMS1_2 > 4 Then
            '第一框：如果有1-1有料，则用;否则，用1-2

            If LKNum_1GMS1_1 > 0 Then
                Car2BoxLoc(0) = 1
                LKNum_1GMS1_1 = LKNum_1GMS1_1 - 1
            ElseIf LKNum_1GMS1_2 > 0 Then
                Car2BoxLoc(0) = 2
                LKNum_1GMS1_2 = LKNum_1GMS1_2 - 1
            End If


            '第二框：如果有1-1有料，则用;否则，用1-2
            If LKNum_1GMS1_1 > 0 Then
                Car2BoxLoc(1) = 1
                LKNum_1GMS1_1 = LKNum_1GMS1_1 - 1
            ElseIf LKNum_1GMS1_2 > 0 Then
                Car2BoxLoc(1) = 2
                LKNum_1GMS1_2 = LKNum_1GMS1_2 - 1
            End If


            '第三框：如果有1-2有料，则用;否则，用1-1
            If LKNum_1GMS1_2 > 0 Then
                Car2BoxLoc(2) = 2
                LKNum_1GMS1_2 = LKNum_1GMS1_2 - 1
            ElseIf LKNum_1GMS1_1 > 0 Then
                Car2BoxLoc(2) = 1
                LKNum_1GMS1_1 = LKNum_1GMS1_1 - 1
            End If
            '第四框：如果有1-2有料，则用;否则，用1-1
            If LKNum_1GMS1_2 > 0 Then
                Car2BoxLoc(3) = 2
                LKNum_1GMS1_2 = LKNum_1GMS1_2 - 1
            ElseIf LKNum_1GMS1_1 > 0 Then
                Car2BoxLoc(3) = 1
                LKNum_1GMS1_1 = LKNum_1GMS1_1 - 1
            End If
            'End If

            Dim _num As Integer = 0
            If GJB_Need_Count > 4 Then
                _num = 4
            Else
                _num = GJB_Need_Count
            End If

            If sumLKnum < _num Then
                _num = sumLKnum
            End If

            For i As Integer = 0 To _num - 1
                Dim _rollerNo As String = ""
                Dim _location1 As String = "-1GMS1-1"
                Dim _location2 As String = "-1GMS1-2"
                Dim _location3 As String = "-1GMS-3"
                Select Case i
                    Case 0, 1
                        _rollerNo = "1#"

                    Case 2, 3
                        _rollerNo = "2#"
                End Select
                _location1 = _rollerNo & _location1
                _location2 = _rollerNo & _location2
                _location3 = _rollerNo & _location3

                If Car2BoxLoc(i) = 1 Then
                    With objActEn
                        ._RGV_ROW_No = rowNo
                        ._RGV_PLC = carPLC
                        ._rollerNo = _rollerNo
                        ._UpDnFLG = "UP"
                        ._fbFLG = "GJB"
                        ._jbgwPLC = plc_1_GJB2
                        ._JBGB_ROW_No = _1GMS1_1_ROW
                        ._location = _location1
                        ._num = 1
                    End With

                    addTask(objActEn)
                ElseIf Car2BoxLoc(i) = 2 Then
                    With objActEn
                        ._RGV_ROW_No = rowNo
                        ._RGV_PLC = carPLC
                        ._rollerNo = _rollerNo
                        ._UpDnFLG = "UP"
                        ._fbFLG = "GJB"
                        ._jbgwPLC = plc_1_GJB2
                        ._JBGB_ROW_No = _1GMS1_2_ROW
                        ._location = _location2
                        ._num = 1
                    End With

                    addTask(objActEn)
                Else
                    With objActEn
                        ._RGV_ROW_No = rowNo
                        ._RGV_PLC = carPLC
                        ._rollerNo = _rollerNo
                        ._UpDnFLG = "UP"
                        ._fbFLG = "GJB"
                        ._jbgwPLC = plc_1_GJB3
                        ._JBGB_ROW_No = _1GMS_3_ROW
                        ._location = _location3
                        ._num = 1
                    End With

                    addTask(objActEn)
                End If
            Next


        Else '当GJP_TYP_1<>GJB_TYP_3时
#Region "RGV移动到1GMS1-1 >= 上4满框"

            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "UP"
                ._fbFLG = "GJB"
                ._jbgwPLC = plc_1_GJB3
                ._JBGB_ROW_No = _1GMS_3_ROW
                ._location = "1#-1GMS-3"
                ._num = 1
            End With
            '1#上两框
            If GJB_Need_Count >= 1 And LKNum_1GM1_3 >= 1 Then
                addTask(objActEn)
                LKNum_1GM1_3 = LKNum_1GM1_3 - 1
            End If
            If GJB_Need_Count >= 2 And LKNum_1GM1_3 >= 1 Then
                addTask(objActEn)
                LKNum_1GM1_3 = LKNum_1GM1_3 - 1
            End If

            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "2#"
                ._UpDnFLG = "UP"
                ._fbFLG = "GJB"
                ._jbgwPLC = plc_1_GJB3
                ._JBGB_ROW_No = _1GMS_3_ROW
                ._location = "2#-1GMS-3"
                ._num = 1
            End With
            '2#上两框
            If GJB_Need_Count >= 3 And LKNum_1GM1_3 >= 1 Then
                addTask(objActEn)
                LKNum_1GM1_3 = LKNum_1GM1_3 - 1
            End If
            If GJB_Need_Count >= 4 And LKNum_1GM1_3 >= 1 Then
                addTask(objActEn)
                LKNum_1GM1_3 = LKNum_1GM1_3 - 1
            End If

#End Region

        End If
    End Sub

    ''' <summary>
    ''' 模式3-B：第4步，把满框的骨架板从RGV车放到接驳工位上
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_GJB_DN_Full_in_Two_4(carPLC As SiemensS7Net, rowNo As Integer)

        Dim Full_Num_In_Car As Integer = 0
        Dim tmpRGV As RGV
        If rowNo = 0 Then
            Full_Num_In_Car = GJB_LK_count_in_Car1
            tmpRGV = Rgv1
        Else
            Full_Num_In_Car = GJB_LK_count_in_Car2
            tmpRGV = Rgv2
        End If
        Dim objActEn As New activeEn
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_2 'plc_2_GJB
            ._JBGB_ROW_No = _2GMX_ROW
            ._location = "2#-2GMX"
            ._num = 1
        End With

        If Full_Num_In_Car >= 3 Then addTask(objActEn)
        If Full_Num_In_Car >= 4 Then addTask(objActEn)
        With objActEn
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = plc_2 'plc_2_GJB
            ._JBGB_ROW_No = _2GMX_ROW
            ._location = "1#-2GMX"
            ._num = 1
        End With
        If Full_Num_In_Car >= 2 Then addTask(objActEn)
        If Full_Num_In_Car >= 1 Then addTask(objActEn)

    End Sub

    ''' <summary>
    ''' 模式6:第2步，把骨架板空框放到一车间
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode6_GJB_DN_Empty_in_One_2(carPLC As SiemensS7Net, rowNo As Integer)
        '取得车上空框的数量
        Dim Empty_Num_In_Car2 As Integer = 0

        If rowNo = 0 Then
            Empty_Num_In_Car2 = GJB_LK_count_in_Car1
        Else
            Empty_Num_In_Car2 = GJB_LK_count_in_Car2
        End If

        Dim objActEn1 As New activeEn
        Dim objActEn2 As New activeEn

        Dim empty_num_in_2 As Integer = 4 - toInt(dtJBGWName(_1GKX_1_ROW)("gwLKNum"))
        Dim empty_num_in_3 As Integer = 4 - toInt(dtJBGWName(_1GKX_3_ROW)("gwLKNum"))

        With objActEn1
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "1#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = Nothing
            ._JBGB_ROW_No = 0
            ._location = ""
            ._num = 1
        End With
        With objActEn2
            ._RGV_ROW_No = rowNo
            ._RGV_PLC = carPLC
            ._rollerNo = "2#"
            ._UpDnFLG = "DN"
            ._fbFLG = "GJB"
            ._jbgwPLC = Nothing
            ._JBGB_ROW_No = 0
            ._location = ""
            ._num = 1
        End With

        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And plc_1_GJB2_AutoMode = True And empty_num_in_2 > 0 Then 'plc_1_GJB2_AutoMode = True Then
            objActEn1._location = "1#-1GKX-1"
            objActEn1._JBGB_ROW_No = _1GKX_1_ROW
            objActEn1._jbgwPLC = plc_1_GJB2
        Else
            objActEn1._jbgwPLC = plc_1_GJB3
            objActEn1._JBGB_ROW_No = _1GKX_3_ROW
            objActEn1._location = "1#-1GKX-3"
        End If
        If Empty_Num_In_Car2 >= 1 Then
            addTask(objActEn1)
        End If
        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And plc_1_GJB2_AutoMode = True And empty_num_in_2 > 1 Then 'plc_1_GJB2_AutoMode = True Then
            objActEn1._location = "1#-1GKX-1"
            objActEn1._JBGB_ROW_No = _1GKX_1_ROW
            objActEn1._jbgwPLC = plc_1_GJB2
        Else
            objActEn1._jbgwPLC = plc_1_GJB3
            objActEn1._JBGB_ROW_No = _1GKX_3_ROW
            objActEn1._location = "1#-1GKX-3"
        End If
        If Empty_Num_In_Car2 >= 2 Then
            addTask(objActEn1)
        End If
        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And plc_1_GJB2_AutoMode = True And empty_num_in_2 >= 3 Then
            objActEn2._location = "2#-1GKX-1"
            objActEn2._JBGB_ROW_No = _1GKX_1_ROW
            objActEn1._jbgwPLC = plc_1_GJB2
        Else
            objActEn2._jbgwPLC = plc_1_GJB3
            objActEn2._JBGB_ROW_No = _1GKX_3_ROW
            objActEn2._location = "2#-1GKX-3"
        End If
        If Empty_Num_In_Car2 >= 3 Then
            addTask(objActEn2)
        End If
        If (GJB_TYPE_IS_EQUALS Or GJB_BOX_TYPE_EQUALS) And plc_1_GJB2_AutoMode = True And empty_num_in_2 >= 4 Then
            objActEn2._location = "2#-1GKX-1"
            objActEn2._JBGB_ROW_No = _1GKX_1_ROW
            objActEn1._jbgwPLC = plc_1_GJB2
        Else
            objActEn2._jbgwPLC = plc_1_GJB3
            objActEn2._JBGB_ROW_No = _1GKX_3_ROW
            objActEn2._location = "2#-1GKX-3"
        End If

        If Empty_Num_In_Car2 >= 4 Then
            addTask(objActEn2)
        End If

    End Sub

#End Region

#Region "模式3-A：Car1"
    ''' <summary>
    ''' 模式3-A：第1步，把1车间的法兰板运上Car1
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_FLB_UP_Full_in_One_1(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn
        'Dim _1FLB_FULL_MES_PLAN_NUM As Integer =   DBNULLtoZero(dtJBGWName(_1FMS_ROW)("mesPlanNum"))

        '在接驳工位上的法兰板数量
        Dim FLB_FULL_NUM As Integer = dtJBGWName(_1FMS_ROW)("gwLKNum")
        If FLB_FULL_NUM > 0 Then
            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "UP"
                ._fbFLG = "FLB"
                ._jbgwPLC = plc_1_FLB
                ._JBGB_ROW_No = _1FMS_ROW
                ._location = "1#-1FMS"
                ._num = 1
            End With
            If FLB_Need_Count >= 1 Then
                addTask(objActEn)
            End If
            If FLB_FULL_NUM > 1 Then
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "UP"
                    ._fbFLG = "FLB"
                    ._jbgwPLC = plc_1_FLB
                    ._JBGB_ROW_No = _1FMS_ROW
                    ._location = "2#-1FMS"
                    ._num = 1
                End With
                If FLB_Need_Count >= 2 Then
                    addTask(objActEn)
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' 模式3-A：第2步，把2车的满框法兰板放到接驳工位上
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_FLB_DN_Empty_in_Two_2(carPLC As SiemensS7Net, rowNo As Integer)
        Dim Full_Num_In_Car As Integer = 0
        ' IIf(dtCarName(rowNo)("in1").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("in2").ToString = "1", 1, 0)
        If rowNo = 0 Then
            Full_Num_In_Car = FLB_LK_count_in_Car1
        Else
            Full_Num_In_Car = FLB_LK_count_in_Car2
        End If
        Dim objActEn As New activeEn
        If Full_Num_In_Car > 0 Then
            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "DN"
                ._fbFLG = "FLB"
                ._jbgwPLC = plc_2 'plc_2_FLB
                ._JBGB_ROW_No = _2FMX_ROW
                ._location = "1#-2FMX"
                ._num = 1
            End With
            addTask(objActEn)
            If Full_Num_In_Car > 1 Then
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "DN"
                    ._fbFLG = "FLB"
                    ._jbgwPLC = plc_2 'plc_2_FLB
                    ._JBGB_ROW_No = _2FMX_ROW
                    ._location = "2#-2FMX"
                    ._num = 1
                End With
                addTask(objActEn)
            End If
        End If

    End Sub

    ''' <summary>
    ''' 模式3-A：第3步，把法兰板空框放到Car1上
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_FLB_UP_Empty_in_Two_3(carPLC As SiemensS7Net, rowNo As Integer)
        Dim objActEn As New activeEn

        Dim _1FKX_Empty_Count As Integer = 2 - dtJBGWName(_1FKX_ROW)("gwLKNum")

        '在接驳工位上的法兰板数量
        Dim _2FKS_count As Integer = dtJBGWName(_2FKS_ROW)("gwLKNum")

        If _2FKS_count > 0 And _1FKX_Empty_Count >= 1 Then
            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "UP"
                ._fbFLG = "FLB"
                ._jbgwPLC = plc_2 'plc_2_FLB
                ._JBGB_ROW_No = _2FKS_ROW
                ._location = "1#-2FKS"
                ._num = 1
            End With

            addTask(objActEn)
            If _2FKS_count > 1 And _1FKX_Empty_Count >= 2 Then
                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "UP"
                    ._fbFLG = "FLB"
                    ._jbgwPLC = plc_2 'plc_2_FLB
                    ._JBGB_ROW_No = _2FKS_ROW
                    ._location = "2#-2FKS"
                    ._num = 1
                End With
                addTask(objActEn)
            End If
        End If
    End Sub

    ''' <summary>
    ''' 模式3-A：第4步，把Car1上的法兰板空框放到接驳工位上
    ''' </summary>
    ''' <param name="carPLC"></param>
    ''' <param name="rowNo"></param>
    Private Sub Mode3_FLB_DN_Empty_in_One_4(carPLC As SiemensS7Net, rowNo As Integer)
        Dim Empty_Num_In_Car1 As Integer = 0
        'IIf(dtCarName(rowNo)("in1").ToString = "1", 1, 0) +
        'IIf(dtCarName(rowNo)("in2").ToString = "1", 1, 0)
        If rowNo = 0 Then
            Empty_Num_In_Car1 = FLB_LK_count_in_Car1
        Else
            Empty_Num_In_Car1 = FLB_LK_count_in_Car2
        End If
        Dim objActEn As New activeEn
        If Empty_Num_In_Car1 > 0 Then

            With objActEn
                ._RGV_ROW_No = rowNo
                ._RGV_PLC = carPLC
                ._rollerNo = "1#"
                ._UpDnFLG = "DN"
                ._fbFLG = "FLB"
                ._jbgwPLC = plc_1_FLB
                ._JBGB_ROW_No = _1FKX_ROW
                ._location = "1#-1FKX"
                ._num = 1
            End With

            addTask(objActEn)
            If Empty_Num_In_Car1 > 1 Then

                With objActEn
                    ._RGV_ROW_No = rowNo
                    ._RGV_PLC = carPLC
                    ._rollerNo = "2#"
                    ._UpDnFLG = "DN"
                    ._fbFLG = "FLB"
                    ._jbgwPLC = plc_1_FLB
                    ._JBGB_ROW_No = _1FKX_ROW
                    ._location = "2#-1FKX"
                    ._num = 1
                End With

                addTask(objActEn)
            End If
        End If
    End Sub
#End Region

#End Region

#Region "任务相关"
    Class activeEn
        Public _fbFLG As String         '骨架板/法兰板区分（GJB：骨架板，FLB：法兰板）
        Public _JBGB_ROW_No As Integer  '接驳工位的DataRow.No.
        Public _jbgwPLC As SiemensS7Net '接驳工位的PLC
        Public _location As String      '接驳工位的工位名称
        Public _num As String           '上料、下料的数量
        Public _RGV_PLC As SiemensS7Net 'RGV的PLC
        Public _RGV_ROW_No As Integer   'RGV的DataRow.No.
        Public _rollerNo As String      'RGV的滚轮号（1#，2#）
        Public _UpDnFLG As String      '上料/下料区分（UP：上料，DN：下料)
    End Class


    ''' <summary>
    ''' 进行任务
    ''' </summary>
    Private Sub start_up_Task()
        If dtTask.Rows.Count = 0 Then
            Exit Sub
        End If

        dtTask.DefaultView.Sort = "task_item_no asc"

        Actioning = True '进行动作状态
        Dim _completed As Boolean = True '是否全部完成

        For i As Integer = 0 To dtTask.Rows.Count - 1
            Dim drTask As DataRow = dtTask.Rows(i)

            '如果处理完，就进行处理（如出错，还要继续处理）
            If drTask("status") <> TASK_STATUS.completed And getSysParam("RGV_AUTO_TASK_PASUE") = "0" Then
                writeInfoLog("任务状态：" & drTask("status"))

                If drTask("status") = TASK_STATUS.Cancel Then
                    _completed = False
                    Exit For
                End If

                '解析任务
                Dim _act As String() = drTask("active").ToString.Split(":")

                    If _act(0) = "TO" Then 'RGV移动
                        If CarMove(drTask) = False Then
                            _completed = False
                            Exit For
                        End If
                    ElseIf _act(0) = "DO" Then '上下料
                        Dim drPre As DataRow
                        drPre = dtTask.Rows(i - 1)
                        If drPre("active").ToString.Split(":")(0) = "TO" Then
                            If CarMove(drPre) = False Then
                                _completed = False
                                Exit For
                            End If
                        End If
                        If Up_Down_Active(drTask) = False Then
                            _completed = False
                            Exit For
                        End If

                    Else '错误的动作
                        writeErrorLog("非移动任务:" & drTask("active"))
                        setTaskRowStatus(drTask, TASK_STATUS.otherError) '更新Task DataTable状态
                        _completed = False
                        Exit For
                    End If
                End If
        Next

        '如果任务全部完成，任务、任务号清空等状态恢复
        If _completed = True Then
            updateTask(TASK_STATUS.AllCompleted)
            dtTask.Clear()
            taskNo = ""
            lblTaskNo.Text = ""
            lblMemo.Text = ""
            Actioning = False
        End If

    End Sub

    ''' <summary>
    ''' 产生新的任务No(年月日时分秒)
    ''' </summary>
    Private Sub newTaskNo(_model As Integer)

        Dim carPLC As SiemensS7Net = Nothing
        Dim carRow As Integer

        If select_Car(carPLC, carRow) = False Then
            Exit Sub
        End If

        '开始任务
        Actioning = True
        Dim _memo As String = ""
        taskNo = (Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second).ToString

        Select Case _model
            Case 1
                _memo = "模式1:(单车)1法兰板2骨架板"
            Case 2
                _memo = "模式2:(双车)1法兰板2骨架板X2"
            Case 3
                _memo = "模式3:(单车)4骨架板"
            Case 4
                _memo = "模式4:(单车)2法兰板"
            Case 5
                _memo = "模式5:(单车)只装空框 1法兰板2骨架板"
            Case 6
                _memo = "模式6:(单车)只装骨架板空框"
        End Select
        newTask(_memo)
        lblTaskNo.Text = taskNo
        lblMemo.Text = _memo
    End Sub
    Private Sub setRGV_Active(rgvNo As String, _activeString As String)
        dtCarName.Rows(rgvNo)("ActiveString") = _activeString
    End Sub

    Private Sub addTask(En As activeEn)
        '_fbFLG       骨架板/法兰板区分（GJB：骨架板，FLB：法兰板）
        '_JBGB_ROW_No 接驳工位的DataRow.No.
        '_jbgwPLC     接驳工位的PLC
        '_location    接驳工位的工位名称
        '_num         上料、下料的数量--无用
        '_RGV_PLC     RGV的PLC
        '_RGV_ROW_No  RGV的DataRow.No.
        '_rollerNo    RGV的滚轮号（1#，2#）/Car-Door 另外：到达车间门前，移动中处理
        '_UpDnFLG     上料/下料区分（UP：上料or开门，DN：下料or关门）
        'If getStation_Status(En._JBGB_ROW_No) = False Then
        '    Exit Sub
        'End If
        Dim intNewRowIndex As Integer = 1
        '移动 TO:XXXX
        If dtTask.Rows.Count > 0 Then
            intNewRowIndex = dtTask.Rows.Count + 1
        End If
        If En._location <> "" Then
            Dim newToRow As DataRow = dtTask.NewRow()
            newToRow("taskNo") = taskNo
            newToRow("task_item_no") = intNewRowIndex
            newToRow("car") = "Car:" & En._RGV_ROW_No + 1
            newToRow("active") = "TO:" & En._location
            newToRow("status") = CInt(TASK_STATUS.wait)
            newToRow("finished_DT") = Now
            dtTask.Rows.Add(newToRow)
            dtTask.AcceptChanges()
            newTaskDetail(newToRow)
        End If

        If En._rollerNo & En._fbFLG & En._UpDnFLG <> "" Then
            '动作 DO:YYYY
            If dtTask.Rows.Count > 0 Then
                intNewRowIndex = dtTask.Rows.Count + 1
            End If
            Dim newDoRow As DataRow = dtTask.NewRow()
            newDoRow("taskNo") = taskNo
            newDoRow("task_item_no") = intNewRowIndex
            newDoRow("car") = "Car:" & En._RGV_ROW_No + 1
            newDoRow("active") = "DO:" & En._rollerNo & En._fbFLG & En._UpDnFLG & ":" & En._JBGB_ROW_No
            newDoRow("status") = CInt(TASK_STATUS.wait)
            newDoRow("finished_DT") = Now
            dtTask.Rows.Add(newDoRow)
            dtTask.AcceptChanges()
            newTaskDetail(newDoRow)
            setCarLKCount(En) '--------更新车上的数量
        End If

    End Sub

    Private Function getStation_Status(_jbgwNo As Integer) As Boolean
        Select Case _jbgwNo
            Case 0, 1
                Return plc_1_FLB_Connected And plc_1_FLB_Connected
            Case 2, 3, 4
                Return plc_1_GJB2_Connected And plc_1_GJB2_Connected
            Case 5, 6
                Return plc_1_GJB3_Connected And plc_1_GJB3_Connected
            Case 7, 8
                Return plc_2_Connected And plc_2F_AutoMode
            Case 9, 10
                Return plc_2_Connected And plc_2G_AutoMode
            Case Else
                Return False
        End Select
    End Function


    Private Sub setCarLKCount(En As activeEn)
        If En._fbFLG = "GJB" Then
            If En._RGV_ROW_No = 0 Then
                If En._UpDnFLG = "UP" Then
                    GJB_LK_count_in_Car1 += 1
                ElseIf En._UpDnFLG = "DN" Then
                    GJB_LK_count_in_Car1 -= 1
                End If
            Else
                If En._UpDnFLG = "UP" Then
                    GJB_LK_count_in_Car2 += 1
                ElseIf En._UpDnFLG = "DN" Then
                    GJB_LK_count_in_Car2 -= 1
                End If
            End If
        End If
        If En._fbFLG = "FLB" Then
            If En._RGV_ROW_No = 0 Then
                If En._UpDnFLG = "UP" Then
                    FLB_LK_count_in_Car1 += 1
                ElseIf En._UpDnFLG = "DN" Then
                    FLB_LK_count_in_Car1 -= 1
                End If
            Else
                If En._UpDnFLG = "UP" Then
                    FLB_LK_count_in_Car2 += 1
                ElseIf En._UpDnFLG = "DN" Then
                    FLB_LK_count_in_Car2 -= 1
                End If
            End If
        End If

    End Sub

    Private Sub setTaskRowStatus(drTask As DataRow, _status As TASK_STATUS)
        drTask("status") = CInt(_status)
        drTask("finished_DT") = Now
        updateTaskDetail(drTask)
    End Sub

    ''' <summary>
    ''' RGV移动
    ''' </summary>
    ''' <param name="drTask">Task内的某行</param>
    Private Function CarMove(drTask As DataRow) As Boolean

        '车的PLC
        Dim carPLC As SiemensS7Net
        If drTask("car").ToString.Split(":")(1).Equals("1") Then
            carPLC = plc_car1
        Else
            carPLC = plc_car2
        End If

        '光栅判断-----------
        Dim arriveResult As OperateResult(Of Integer) = carPLC.ReadInt32("DB17.4.0")
        If arriveResult.IsSuccess = True Then
            Select Case arriveResult.Content
                Case 101, 102, 103 '1车间法兰板
                    If plc_1_FLB_Connected AndAlso plc_1_FLB.ReadBool("DB1.DBX20.1").IsSuccess Then
                        If plc_1_FLB.ReadBool("DB1.DBX20.1").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If
                Case 3, 103 '1车间A线
                    If plc_1_GJB2_Connected AndAlso plc_1_GJB2.ReadBool("DB15.DBX20.5").IsSuccess Then
                        If plc_1_GJB2.ReadBool("DB15.DBX20.5").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If
                Case 4, 104 '1车间空线
                    If plc_1_GJB2_Connected AndAlso plc_1_GJB2.ReadBool("DB15.DBX20.7").IsSuccess Then
                        If plc_1_GJB2.ReadBool("DB15.DBX20.7").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If
                Case 5, 105 '1车间B线
                    If plc_1_GJB2_Connected AndAlso plc_1_GJB2.ReadBool("DB15.DBX20.6").IsSuccess Then
                        If plc_1_GJB2.ReadBool("DB15.DBX20.6").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If
                Case 9, 109, 110 '1车间骨架板
                    If plc_2_Connected AndAlso plc_1_GJB3_Connected AndAlso plc_1_GJB3.ReadBool("DB1.DBX20.1").IsSuccess Then
                        If plc_1_GJB3.ReadBool("DB1.DBX20.1").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If

                Case 11， 12, 111, 112 '2车间法兰板
                    If plc_2_Connected AndAlso plc_2.ReadBool("DB200.DBX121.3").IsSuccess Then
                        If plc_2.ReadBool("DB200.DBX121.3").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If
                Case 13， 14, 113, 114 '2车间骨架板
                    If plc_2.ReadBool("DB200.DBX121.4").IsSuccess Then
                        If plc_2.ReadBool("DB200.DBX121.4").Content = False Then
                            setTaskRowStatus(drTask, TASK_STATUS.rasterAllowError)
                            Return False
                        End If
                    End If

            End Select
        End If
        '光栅判断-----------

        setTaskRowStatus(drTask, TASK_STATUS.begin) '更新Task DataTable状态

        Dim _distination() As String = drTask("active").ToString.Split(":")

        '目的地的寄存地址
        Dim _distination_Memory_address As String = modWinApi.GetIniFile("address", _distination(1), "", Me.g_szInitFileName)
        Dim _distination_Code As String = modWinApi.GetIniFile("Station", _distination(1), "", Me.g_szInitFileName)

        'RGV移动(当不在目的地的时候移动)
        If carPLC.ReadInt32("DB17.4.0").Content <> CInt(_distination_Code) Then
            writeInfoLog(String.Format("{0},移动至{1}的指令发送", drTask("car"), drTask("active")))
            If car_send(carPLC, _distination_Memory_address, True) = False Then '发送【移动指令】给RGV的PLC
                setTaskRowStatus(drTask, TASK_STATUS.carMoveError) '更新Task DataTable状态
                Return False
            End If
        Else
            writeInfoLog(String.Format("{0},到达{1}", drTask("car"), _distination(1)))
            setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
            Return True
        End If

        '等待车辆到达
        Dim _arrivedTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
                                                                                     Return carPLC.WaitAsync("DB17.4.0", CInt(_distination_Code), 500, -1)
                                                                                 End Function)
        '远程急停
        Dim _remoteStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
                                                                                        Return carPLC.WaitAsync("DB16.4.4", True, 500, -1)
                                                                                    End Function)
        '本地急停
        Dim _carStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
                                                                                     Return carPLC.WaitAsync("DB17.27.2", True, 500, -1)
                                                                                 End Function)
        '任务数组
        Dim tasks As Task() = {_arrivedTaskResult} ', _remoteStopTaskResult, _carStopTaskResult}

        '等待任务完成
        Dim _CompletedIndex As Integer = Task.WaitAny(tasks, taskWaitTimeOut)

        '当其中任何一个任务结束时，进行以下处理
        Select Case _CompletedIndex

            Case 0  '车辆到达
                If _arrivedTaskResult.Result.IsSuccess Then '正常完了
                    Try
                        writeInfoLog(String.Format("{0},到达{1}", drTask("car"), _distination(1)))
                        setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
                        Return True

                    Catch ex As Exception
                        Return True
                    End Try

                Else
                    writeErrorLog(String.Format("{0},移动异常：{1}:{2}", drTask("car"), _arrivedTaskResult.Result.ErrorCode, _arrivedTaskResult.Result.Message))
                    Return False
                End If
            Case 1  '远程急停
                If _remoteStopTaskResult.Result.IsSuccess Then
                    writeErrorLog(String.Format("{0},远程急停", drTask("car")))
                    setTaskRowStatus(drTask, TASK_STATUS.remoteCarStop) '更新Task DataTable状态
                    Return False
                End If
            Case 2  '本地急停
                If _carStopTaskResult.Result.IsSuccess Then
                    writeErrorLog(String.Format("{0},车辆【急停】按钮", drTask("car")))
                    setTaskRowStatus(drTask, TASK_STATUS.carStop) '更新Task DataTable状态
                    Return False
                End If
            Case Else
                writeErrorLog(String.Format("{0},车辆移动超时", drTask("car")))
                setTaskRowStatus(drTask, TASK_STATUS.carMoveError) '更新Task DataTable状态
                Return False
        End Select
        Return False
    End Function

    ''' <summary>
    ''' RGV及接驳工位-上下料动作
    ''' </summary>
    Private Function Up_Down_Active(drTask As DataRow) As Boolean
        Dim _active_address As String = ""

        'DO内容1： ◉DO:1#GJBUP:2
        'DO内容2： ◉DO:DoorDN:
        Dim act() As String = drTask("active").ToString.Split(":")

        Dim carPLC As SiemensS7Net
        If drTask("car").ToString.Split(":")(1).Equals("1") Then
            carPLC = plc_car1
        Else
            carPLC = plc_car2
        End If
        Dim _do_active As String = act(1) '接驳工位的任务 例如：1#GJBUP or DoorDN
        setTaskRowStatus(drTask, TASK_STATUS.begin) '更新Task DataTable状态
        '================================关车门================================
        If _do_active = "DoorDN" Then

            _active_address = modWinApi.GetIniFile("active", _do_active, "", Me.g_szInitFileName)

            '关门操作
            If car_send(carPLC, _active_address, True) = False Then
                setTaskRowStatus(drTask, TASK_STATUS.carDoorError) '更新Task DataTable状态
                writeErrorLog("上下料前，车门打不开！！！")
                setSysParam("RGV_AUTO_ERROR", RGV_ERROR_2)
                Return False
            End If

            '等待关门结束
            Dim _waitCarDoorClose As OperateResult(Of TimeSpan) = carPLC.Wait("DB17.26.2", True, 500, 20 * 1000) '20秒内把门关闭
            If _waitCarDoorClose.IsSuccess = False Then
                setTaskRowStatus(drTask, TASK_STATUS.carDoorError) '更新Task DataTable状态
                Return False
            Else
                setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
                Return True
            End If
        End If
        '================================关车门 以外的操作================================
        '分解任务内容
        Dim _rollerNo As String = act(1).Substring(0, 2) '滚轮号码 如：1#/2#
        Dim _fbFLG As String = act(1).Substring(2, 3)    '法骨分类 如：GJB/FLB
        Dim _UpDnFLG As String = act(1).Substring(5, 2)  '上 下 料 如：Up/Dn
        Dim _JBGB_ROW_No As Integer = CInt(act(2))

        '接驳工位的PLC
        Dim _Station_PLC As SiemensS7Net = Nothing

        '确定:接驳工位的PLC
        Select Case _JBGB_ROW_No
            Case _1FKX_ROW, _1FMS_ROW
                _Station_PLC = plc_1_FLB
            Case _1GMS1_1_ROW, _1GKX_1_ROW, _1GMS1_2_ROW
                _Station_PLC = plc_1_GJB2
            Case _1GMS_3_ROW, _1GKX_3_ROW
                _Station_PLC = plc_1_GJB3
            Case _2FMX_ROW, _2FKS_ROW, _2GMX_ROW, _2GKS_ROW
                _Station_PLC = plc_2
        End Select
        '===============================上下料前--打开车门===============================
        'TODO:打开车门
        '门上升到位   Bool	26.1
        '门下降到位   Bool	26.2
        If carPLC.ReadBool("DB17.26.2").Content = True Then

            _active_address = modWinApi.GetIniFile("active", "DoorUP", "", Me.g_szInitFileName)
            car_send(carPLC, _active_address, True)

            '等待门开
            Dim _waitCarDoorOpen As OperateResult(Of TimeSpan) = carPLC.Wait("DB17.26.1", True, 500, 20 * 1000) '20秒内把门打开
            If _waitCarDoorOpen.IsSuccess = False Then
                setTaskRowStatus(drTask, TASK_STATUS.carDoorError) '更新Task DataTable状态
                Return False
            End If
        End If
        '================================下料框================================
        If _UpDnFLG = "DN" Then
            Dim _isSuccessed As Boolean = down_RGV_and_Station(drTask, carPLC, _Station_PLC)
            'If _isSuccessed Then
            '    If _fbFLG = "FLB" And _JBGB_ROW_No = _2FMX_ROW Then
            '        setSysParam("FLB_need_Count_For_YZZ", getSysParam("FLB_need_Count_For_YZZ") - 1)
            '    End If
            '    If _fbFLG = "GJB" And _JBGB_ROW_No = _2GMX_ROW Then
            '        setSysParam("GJB_need_Count_For_YZZ", getSysParam("GJB_need_Count_For_YZZ") - 1)
            '        setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
            '    End If
            'End If
            If _isSuccessed Then
                If _fbFLG = "GJB" And _JBGB_ROW_No = _2GMX_ROW Then
                    setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
                End If
            End If
            Return _isSuccessed
        End If
        '================================上料框================================
        If _UpDnFLG = "UP" Then
            Dim _isSuccessed As Boolean = up_RGV_and_Station(drTask, carPLC, _Station_PLC)
            'If _isSuccessed Then
            '    If _fbFLG = "GJB" And _JBGB_ROW_No = _2GMX_ROW Then
            '        setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
            '    End If
            'End If
            If _isSuccessed Then
                If _fbFLG = "FLB" And _JBGB_ROW_No = _1FMS_ROW Then
                    setSysParam("FLB_need_Count_For_YZZ", CInt(getSysParam("FLB_need_Count_For_YZZ")) - 1)
                    nudFLBNeed.Value = getSysParam("FLB_need_Count_For_YZZ")
                    lblsysFLBNeed.Text = getSysParam("FLB_need_Count_For_YZZ")
                End If
                If _fbFLG = "GJB" And
                (_JBGB_ROW_No = _1GMS1_1_ROW Or _JBGB_ROW_No = _1GMS1_2_ROW Or _JBGB_ROW_No = _1GMS_3_ROW) Then
                    setSysParam("GJB_need_Count_For_YZZ", CInt(getSysParam("GJB_need_Count_For_YZZ")) - 1) '更新需要的骨架板数量

                    'setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
                    NudGJBNeed.Value = getSysParam("GJB_need_Count_For_YZZ")
                    lblsysGJBNeed.Text = getSysParam("GJB_need_Count_For_YZZ")
                End If
                If _fbFLG = "GJB" And _JBGB_ROW_No = _2GKS_ROW Then

                    If CInt(lblsysGJBNeed.Text) > 0 Then
                        If getSysParam("YZZ_GJB_BOX_Num") > 0 Then
                            setSysParam("YZZ_GJB_BOX_Num", CInt(getSysParam("YZZ_GJB_BOX_Num")) - 1) '更新需要的骨架板数量
                            nudGJBnum.Value = getSysParam("YZZ_GJB_BOX_Num")
                            lblsysGJBnum.Text = getSysParam("YZZ_GJB_BOX_Num")
                        End If

                    End If
                    'setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
                End If
            End If
            Return _isSuccessed
        End If
        Return True
    End Function

    ''' <summary>
    ''' '******下料动作******
    ''' </summary>
    ''' <param name="drTask">任务</param>
    ''' <param name="carPLC">车的PLC</param>
    ''' <param name="_Station_PLC">接驳工位PLC</param>
    ''' <returns>成功是否</returns>
    Private Function down_RGV_and_Station(drTask As DataRow, carPLC As SiemensS7Net, _Station_PLC As SiemensS7Net) As Boolean


        Dim act() As String = drTask("active").ToString.Split(":")

        '分解任务内容
        Dim _rollerNo As String = act(1).Substring(0, 2) '滚轮号码 如：1#/2#
        Dim _fbFLG As String = act(1).Substring(2, 3)    '法骨分类 如：GJB/FLB
        Dim _UpDnFLG As String = act(1).Substring(5, 2)  '上 下 料 如：Up/Dn
        Dim _JBGB_ROW_No As Integer = CInt(act(2))

        'RGV车的动作
        Dim _RGV_Active_address As String = modWinApi.GetIniFile("active", act(1), "", Me.g_szInitFileName)
        '================================下料框================================

        Dim jbgw_cmd As String = ""
        Dim wait_allow_XL As String = ""
        Dim jbgw_stop As String = ""
        Dim Car_DN_CMD As String
        Dim DisboardFinishAddress As String = ""
        Select Case _JBGB_ROW_No
            Case _1FKX_ROW, _1GKX_3_ROW
                jbgw_cmd = "DB1.DBX0.1"              '下料
                wait_allow_XL = "DB1.DBX0.3"         '允许下料 
                jbgw_stop = "DB1.DBX0.4"             '急停
                DisboardFinishAddress = "DB1.DBX1.5" '下料完成
            Case _1GKX_1_ROW
                jbgw_cmd = "DB15.DBX0.4"
                wait_allow_XL = "DB15.DBX0.5"
                jbgw_stop = "DB15.DBX0.6"
                DisboardFinishAddress = "DB15.DBX1.6"
            Case _2FMX_ROW
                jbgw_cmd = "DB200.DBX77.0"
                wait_allow_XL = "DB200.DBX111.1"
                jbgw_stop = "DB200.DBX111.3"
                DisboardFinishAddress = "DB200.DBX111.2"
            Case _2GMX_ROW
                jbgw_cmd = "DB200.DBX77.3"
                wait_allow_XL = "DB200.DBX111.4"
                jbgw_stop = "DB200.DBX111.6"
                DisboardFinishAddress = "DB200.DBX111.5"
        End Select

        '等待允许下料 
        Dim _wait_allow_XL As OperateResult(Of TimeSpan) = _Station_PLC.Wait(wait_allow_XL, True, 500, taskWaitTimeOut)
        If _wait_allow_XL.IsSuccess Then
            '******RGV 下料 
            If car_send(carPLC, _RGV_Active_address, True) = False Then '下料
                setTaskRowStatus(drTask, TASK_STATUS.carDNerror) '更新Task DataTable状态
                Return False
            End If

            '******接驳工位 下料
            If _Station_PLC.Write(jbgw_cmd, True).IsSuccess = False Then
                setTaskRowStatus(drTask, TASK_STATUS.stationDNerror) '更新Task DataTable状态
                writeErrorLog(String.Format("接驳工位：{0} 写入失败,地址：{1} 值：{2}", getStationName(_JBGB_ROW_No), jbgw_cmd, "True"))
                '接驳工位 下料 错误时 ---》 RGV 急停
                car_send(carPLC, "DB16.4.4", True)
                Thread.Sleep(500)
                car_send(carPLC, "DB16.4.4", False)
                Return False
            Else
                Thread.Sleep(100)
                If _Station_PLC.Write(jbgw_cmd, False).IsSuccess = False Then
                    writeErrorLog(String.Format("接驳工位：{0} 写入失败,地址：{1} 值：{2}", getStationName(_JBGB_ROW_No), jbgw_cmd, "False"))
                End If
            End If

            '等待RGV 下料完成 
            Dim _carDNfinishAddress As String = "" 'RGV上下料完了的寄存地址
            If _rollerNo = "1#" Then
                _carDNfinishAddress = "DB17.27.4"
                Car_DN_CMD = "DB16.7.4"
            Else
                _carDNfinishAddress = "DB17.27.3"
                Car_DN_CMD = "DB16.7.5"
            End If
#Region "删除"


            ''等待RGV 下料完成
            'Dim _carDisboardTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
            '                                                                                 Return carPLC.WaitAsync(_carDNfinishAddress, True, 500, -1)
            '                                                                             End Function)

            ''等待RGV 远程急停
            'Dim _carRemoteStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
            '                                                                                   Return carPLC.WaitAsync("DB16.4.4", True, 500, -1)
            '                                                                               End Function)
            ''等待RGV 本地急停
            'Dim _carStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
            '                                                                             Return carPLC.WaitAsync("DB17.27.2", True, 500, -1)
            '                                                                         End Function)

            ''等待接驳工位 下料完成 
            'Dim _stationDisboardTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
            '                                                                                     Return _Station_PLC.WaitAsync(DisboardFinishAddress, True, 200, -1)
            '                                                                                 End Function)

            ''等待接驳工位 远程急停
            'Dim _stationStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
            '                                                                                 Return _Station_PLC.WaitAsync(jbgw_stop, True, 500, -1)
            '                                                                             End Function)

            ''=============== 接驳工位 下料 ================
            ''接驳工位 数组
            'Dim _stationTask() As Task = {_stationDisboardTaskResult} ', _stationStopTaskResult}
            'Dim _stationCompletedIndex As Integer = Task.WaitAny(_stationTask, taskWaitTimeOut)
            'writeInfoLog("*****开始监控接驳工位下料是否完成*****")
            'Select Case _stationCompletedIndex
            '    Case 0  '接驳工位 下料完成
            '        If _stationDisboardTaskResult.Result.IsSuccess Then '■正常完了■■■
            '            setTaskRowStatus(drTask, TASK_STATUS.stationUPcompleted) '更新Task DataTable状态
            '            writeInfoLog(String.Format("接驳工位:{0},下料完成", getStationName(act(2))))
            '            '通知RGV 下料完成 
            '            If carPLC.Write(Car_DN_CMD, True).IsSuccess Then
            '                writeInfoLog("发给RGV下料完成信号True 完成")
            '            Else
            '                writeErrorLog("发给RGV下料完成信号True 失败")
            '                setTaskRowStatus(drTask, TASK_STATUS.carDNerror)
            '                Return False
            '            End If
            '            Thread.Sleep(500)
            '            If carPLC.Write(Car_DN_CMD, False).IsSuccess Then
            '                writeInfoLog("发给RGV下料完成信号False 完成")
            '                setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
            '                Return True
            '            Else
            '                writeErrorLog("发给RGV下料完成信号False 失败")
            '                setTaskRowStatus(drTask, TASK_STATUS.carDNerror)
            '                Return False
            '            End If
            '            'setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
            '            'Return True
            '        Else
            '            setTaskRowStatus(drTask, TASK_STATUS.stationDNerror) '更新Task DataTable状态
            '            writeErrorLog(String.Format("接驳工位:{0},下料异常：{1}:{2}", getStationName(act(2)), _stationDisboardTaskResult.Result.ErrorCode, _stationDisboardTaskResult.Result.Message))
            '            Return False
            '        End If
            '    Case 1  '远程急停
            '        If _stationStopTaskResult.Result.IsSuccess Then
            '            setTaskRowStatus(drTask, TASK_STATUS.remoteStationStop) '更新Task DataTable状态
            '            writeErrorLog(String.Format("接驳工位:{0},急停", getStationName(act(2))))
            '            Return False
            '        End If
            '    Case Else
            '        setTaskRowStatus(drTask, TASK_STATUS.stationDNerror) '更新Task DataTable状态
            '        writeErrorLog(String.Format("接驳工位:{0},下料时出错", getStationName(act(2))))
            '        Return False
            'End Select
            'writeInfoLog(">>>>接驳工位下料完成<<<<<")
#End Region
#Region "删除2"
            '=============== RGV 下料 ================

            ''车辆任务 数组
            'Dim _carTask() As Task = {_carDisboardTaskResult, _carRemoteStopTaskResult, _carStopTaskResult}

            'Dim _carCompletedIndex As Integer = Task.WaitAny(_carTask, taskWaitTimeOut)

            ''当其中任何一个任务结束时，进行以下处理
            'Select Case _carCompletedIndex
            '    Case 0  '车辆下料完成
            '        If _carDisboardTaskResult.Result.IsSuccess Then '正常完了
            '            writeInfoLog(String.Format("{0},下料完成：{1}", drTask("car"), act(1)))
            '            'setTaskRowStatus(drTask, TASK_STATUS.carDNcompleted) '更新Task DataTable状态
            '            setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
            '            Return True
            '        Else
            '            setTaskRowStatus(drTask, TASK_STATUS.carDNerror) '更新Task DataTable状态
            '            writeErrorLog(String.Format("{0},下料异常：{1}:{2}", drTask("car"), _carDisboardTaskResult.Result.ErrorCode, _carDisboardTaskResult.Result.Message))
            '            Return False
            '        End If
            '    Case 1  '远程急停
            '        If _carRemoteStopTaskResult.Result.IsSuccess Then
            '            writeErrorLog(String.Format("{0},远程急停", drTask("car")))
            '            setTaskRowStatus(drTask, TASK_STATUS.remoteCarStop) '更新Task DataTable状态
            '            Return False
            '        End If
            '    Case 2  '本地急停
            '        If _carStopTaskResult.Result.IsSuccess Then
            '            writeErrorLog(String.Format("{0},车辆【急停】按钮", drTask("car")))
            '            setTaskRowStatus(drTask, TASK_STATUS.carStop) '更新Task DataTable状态
            '            Return False
            '        End If
            '    Case Else
            '        writeErrorLog(String.Format("{0},下料时出错", drTask("car")))
            '        setTaskRowStatus(drTask, TASK_STATUS.carDNerror) '更新Task DataTable状态
            '        Return False
            'End Select
#End Region

            '等待下料完成
            Dim thp As New thParam
            thp.Car_DN_CMD = Car_DN_CMD
            thp.DN_CMD = DisboardFinishAddress
            thp._JBGW_PLC = _Station_PLC
            thp._RGV_PLC = carPLC
            thp.scram_CAR = "DB16.4.4"
            thp.scram_JBGW = jbgw_stop
            If thDNWait_Elapsed(thp) Then
                setTaskRowStatus(drTask, TASK_STATUS.completed)
                Return True
            Else
                setTaskRowStatus(drTask, TASK_STATUS.stationDNerror) '更新Task DataTable状态
                Return False
            End If

        Else
            setTaskRowStatus(drTask, TASK_STATUS.stationAllowError) '更新Task DataTable状态
            writeErrorLog(String.Format("接驳工位:{0},等待工位的【允许下料】超时：{1}:{2}", getStationName(act(2))， _wait_allow_XL.ErrorCode, _wait_allow_XL.Message))
            Return False
        End If

        writeErrorLog(String.Format("接驳工位:{0},下料失败！", getStationName(act(2))))
        Return False

    End Function


    ''' <summary>
    ''' '******上料动作******
    ''' </summary>
    ''' <param name="drTask">任务</param>
    ''' <param name="carPLC">车的PLC</param>
    ''' <param name="_Station_PLC">接驳工位PLC</param>
    ''' <returns>成功是否</returns>
    Private Function up_RGV_and_Station(drTask As DataRow, carPLC As SiemensS7Net, _Station_PLC As SiemensS7Net) As Boolean

        Dim act() As String = drTask("active").ToString.Split(":")

        '分解任务内容
        Dim _rollerNo As String = act(1).Substring(0, 2) '滚轮号码 如：1#/2#
        Dim _fbFLG As String = act(1).Substring(2, 3)    '法骨分类 如：GJB/FLB
        Dim _UpDnFLG As String = act(1).Substring(5, 2)  '上 下 料 如：Up/Dn
        Dim _JBGB_ROW_No As Integer = CInt(act(2))

        Dim jbgw_cmd_st As String = ""
        Dim jbgw_cmd_ed As String = ""
        Dim jbgw_stop As String = ""
        Dim jbgw_allow_UP As String = ""
        '根据不同的PLC ，取不同的寄存地址
        Select Case _JBGB_ROW_No
            Case _1FMS_ROW, _1GMS_3_ROW
                jbgw_allow_UP = "DB1.DBX1.6"
                jbgw_cmd_st = "DB1.DBX0.0"
                jbgw_cmd_ed = "DB1.DBX0.2"
                jbgw_stop = "DB1.DBX0.4"
            Case _1GMS1_1_ROW
                jbgw_allow_UP = "DB15.DBX20.2"
                jbgw_cmd_st = "DB15.DBX0.0"
                jbgw_cmd_ed = "DB15.DBX0.1"
                jbgw_stop = "DB15.DBX0.6"
            Case _1GMS1_2_ROW
                jbgw_allow_UP = "DB15.DBX20.3"
                jbgw_cmd_st = "DB15.DBX0.2"
                jbgw_cmd_ed = "DB15.DBX0.3"
                jbgw_stop = "DB15.DBX0.6"
            Case _2FKS_ROW
                jbgw_allow_UP = "DB200.DBX120.6"
                jbgw_cmd_st = "DB200.DBX77.1"
                jbgw_cmd_ed = "DB200.DBX77.2"
                jbgw_stop = "DB200.DBX111.3"
            Case _2GKS_ROW
                jbgw_allow_UP = "DB200.DBX120.7"
                jbgw_cmd_st = "DB200.DBX77.4"
                jbgw_cmd_ed = "DB200.DBX77.5"
                jbgw_stop = "DB200.DBX111.6"
        End Select

        'RGV车的动作================================================================================================
        Dim _RGV_Active_address As String = modWinApi.GetIniFile("active", act(1), "", Me.g_szInitFileName)

        '等待接驳工位允许上料信号
        If _Station_PLC.ReadBool(jbgw_allow_UP).Content = False Then
            writeInfoLog("等待接驳工位允许上料信号=false")
            setTaskRowStatus(drTask, TASK_STATUS.stationUPerror) '更新Task DataTable状态
        End If

        Dim stationWaitAllowResult As OperateResult(Of TimeSpan) = _Station_PLC.Wait(jbgw_allow_UP, True, 500, taskWaitTimeOut)

        If stationWaitAllowResult.IsSuccess = False Then
            writeInfoLog("等待2分钟-接驳工位允许上料信号=false")
            setTaskRowStatus(drTask, TASK_STATUS.stationUPerror) '更新Task DataTable状态
            Return False
        Else
            '继续下面的动作
            writeInfoLog("接驳工位允许上料信号=true")
        End If

        'RGV 上料====================================================================================================
        If car_send(carPLC, _RGV_Active_address, True) = False Then '上料 
            writeInfoLog("给RGV上料信号失败")
            setTaskRowStatus(drTask, TASK_STATUS.carUPerror) '更新Task DataTable状态
            Return False
        End If

        '接驳工位 上料====================================================================================================
        If _Station_PLC.Write(jbgw_cmd_st, True).IsSuccess = False Then
            writeInfoLog("给接驳工位上料信号 写入True 失败")
            Threading.Thread.Sleep(500)
            _Station_PLC.Write(jbgw_cmd_st, False)

            '接驳工位 上料 错误时 ---》 RGV 急停
            car_send(carPLC, "DB16.4.4", True)
            Threading.Thread.Sleep(500)
            car_send(carPLC, "DB16.4.4", False)

            setTaskRowStatus(drTask, TASK_STATUS.stationUPerror) '更新Task DataTable状态
            Return False
        Else
            writeInfoLog("给接驳工位上料信号 写入True 成功")
            Threading.Thread.Sleep(500)

            If _Station_PLC.Write(jbgw_cmd_st, False).IsSuccess = False Then
                writeInfoLog("给接驳工位上料信号 写入False 失败")
                setTaskRowStatus(drTask, TASK_STATUS.stationUPerror) '更新Task DataTable状态
                Return False
            Else
                writeInfoLog("给接驳工位上料信号 写入False 成功")
            End If

        End If

        '等待RGV 上料完成 地址********TODO：追加上料完成地址
        Dim _carfinishAddress As String = "" 'RGV上下料完了的寄存地址
        If _rollerNo = "1#" Then
            _carfinishAddress = "DB17.27.6"
        Else
            _carfinishAddress = "DB17.27.7"
        End If
        writeInfoLog("*****等待RGV上料是否完成:" & _carfinishAddress)
#Region "删除"


        '等待RGV 上料完成
        'Dim _carOnTOTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                             Return carPLC.WaitAsync(_carfinishAddress, True, 200, -1)
        '                                                                         End Function)

        ''等待RGV 远程急停
        'Dim _carRemoteStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                                   Return carPLC.WaitAsync("DB16.4.4", True, 500, -1)
        '                                                                               End Function)
        ''等待RGV 本地急停
        'Dim _carStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        '                                                                             Return carPLC.WaitAsync("DB17.27.2", True, 500, -1)
        '                                                                         End Function)
        ''等待接驳工位 远程急停
        ''Dim _stationStopTaskResult As Task(Of OperateResult(Of TimeSpan)) = Task.Run(Function()
        ''                                                                                 Return _Station_PLC.WaitAsync(jbgw_stop, True, 500, -1)
        ''                                                                             End Function)

        'Dim tasks() As Task = {_carOnTOTaskResult} ', _carRemoteStopTaskResult, _carStopTaskResult} ', _stationStopTaskResult}

        'Dim taskCompletedIndex As Integer = Task.WaitAny(tasks, taskWaitTimeOut)
        'writeInfoLog("*****开始监控RGV上料是否完成*****")
        'Select Case taskCompletedIndex
        '    Case 0
        '        If _carOnTOTaskResult.Result.IsSuccess Then
        '            Dim i As Integer = 1

        '            While i <= 10
        '                '接驳工位上料完成信号 true
        '                If _Station_PLC.Write(jbgw_cmd_ed, True).IsSuccess Then
        '                    writeInfoLog("接驳工位上料完成 写入True  成功")
        '                    Threading.Thread.Sleep(500)

        '                    '接驳工位上料完成信号 false
        '                    If _Station_PLC.Write(jbgw_cmd_ed, False).IsSuccess Then
        '                        writeInfoLog("接驳工位上料完成信号 写入False  成功")
        '                        setTaskRowStatus(drTask, TASK_STATUS.completed) '更新Task DataTable状态
        '                        writeInfoLog(String.Format("{0},上料完成", drTask("car")))

        '                        'setGJB_Num_in_YZZ(drTask) '更新2车间线上的骨架板数量
        '                        Return True
        '                        Exit While
        '                    Else
        '                        i = i + 1
        '                        writeInfoLog("接驳工位上料完成信号 写入False  失败")
        '                    End If
        '                Else
        '                    i = i + 1
        '                    writeInfoLog("接驳工位上料完成信号 写入True  失败")
        '                End If
        '            End While

        '        Else
        '            setTaskRowStatus(drTask, TASK_STATUS.carUPerror) '更新Task DataTable状态
        '            writeErrorLog(String.Format("{0},上料错误", drTask("car")))
        '            Return False
        '        End If
        '    Case 1  '车 远程急停
        '        If _carRemoteStopTaskResult.Result.IsSuccess Then
        '            writeErrorLog(String.Format("{0},远程急停", drTask("car")))
        '            setTaskRowStatus(drTask, TASK_STATUS.remoteCarStop) '更新Task DataTable状态
        '            Return False
        '        End If
        '    Case 2  '车 本地急停
        '        If _carStopTaskResult.Result.IsSuccess Then
        '            writeErrorLog(String.Format("{0},车辆【急停】按钮", drTask("car")))
        '            setTaskRowStatus(drTask, TASK_STATUS.carStop) '更新Task DataTable状态
        '            Return False
        '        End If
        '        'Case 3  '接驳工位 远程急停
        '        '    If _stationStopTaskResult.Result.IsSuccess Then
        '        '        setTaskRowStatus(drTask, TASK_STATUS.remoteStationStop) '更新Task DataTable状态
        '        '        writeErrorLog(String.Format("接驳工位:{0},急停", getStationName(act(2))))
        '        '        Return False
        '        '    End If
        '    Case Else
        '        writeErrorLog(String.Format("{0},上料出错", drTask("car")))
        '        setTaskRowStatus(drTask, TASK_STATUS.carUPerror) '更新Task DataTable状态
        '        Return False
        'End Select
        'writeInfoLog(">>>>RGV上料完成<<<<<")
        'writeErrorLog(String.Format("接驳工位:{0},上料失败！", getStationName(act(2))))
        'Return False
#End Region
        '等待下料完成
        Dim thp As New thParam
        thp.DN_CMD = jbgw_cmd_ed
        thp._JBGW_PLC = _Station_PLC
        thp._RGV_PLC = carPLC
        thp._rollerNo = _rollerNo
        thp.scram_CAR = "DB16.4.4"
        thp.scram_JBGW = jbgw_stop

        If tmUpWait_Elapsed(thp) Then
            setTaskRowStatus(drTask, TASK_STATUS.completed)
            Return True
        Else
            setTaskRowStatus(drTask, TASK_STATUS.stationUPerror)
            Return False
        End If

    End Function
    Private Class thParam
        Public Car_DN_CMD As String
        Public DN_CMD As String
        Public _JBGW_PLC As SiemensS7Net
        Public _RGV_PLC As SiemensS7Net
        Public _rollerNo As String
        Public scram_CAR As String '车 急停
        Public scram_JBGW As String '接驳工位 急停
    End Class

    Private Sub canelTaskDetail()
        'Dim szSql As String = ""
        'szSql &= "update sf.b_rgv_task_detail "
        'szSql &= "set status=98 ,finished_dt=? "
        'szSql &= "where taskno=(select taskno from sf.b_rgv_task where status<>100) and status<>2"
        'Dim updatenum As Integer = DBacs.ExecuteUpdateSql(szSql, -1, Now)
        'writeInfoLog("任务明细修改成【取消】的条数" & updatenum & " : " & dtTask.Rows(0)("taskno"))
        'For Each drtaskdetail As DataRow In dtTask.Rows
        '    If drtaskdetail("status").ToString <> "2" Then
        '        drtaskdetail("status") = TASK_STATUS.Cancel
        '        writeInfoLog(drtaskdetail("task_item_no") & "-> Cancel")
        '    End If
        'Next
        'dtTask.AcceptChanges()

        'szSql = "update sf.b_rgv_task "
        'szSql &= "set status=98 ,finished_dt=? "
        'szSql &= "where taskno="
        'updatenum = DBacs.ExecuteUpdateSql(szSql, -1, Now, dtTask.Rows(0)("taskno"))
        'writeInfoLog("任务修改成【取消】的条数" & updatenum & " : " & dtTask.Rows(0)("taskno"))
        For Each drtaskdetail As DataRow In dtTask.Rows
            If drtaskdetail("status").ToString <> "2" Then
                setTaskRowStatus(drtaskdetail, TASK_STATUS.Cancel)
            End If
        Next

    End Sub
    Private Function thDNWait_Elapsed(pa As thParam) As Boolean
        Dim JBGW下料完成 As Boolean = False
        Dim rtn As Boolean
        Dim CarDoorOpen As Boolean
        'Dim Car下料完成 As Boolean = False
        'Dim Car_DN_CMD As String
        '远程下料完成1#	Bool	DB16.7.4
        '远程下料完成2#	Bool	DB16.7.5

        'If Current_StationNo = 0 Then
        '    Car_DN_CMD = "DB16.7.4"
        'Else
        '    Car_DN_CMD = "DB16.7.5"
        'End If
        ' <param name="Car_DN_CMD">RGV下料完成信号地址</param>
        ' <param name="DN_CMD">接驳工位下料完成信号地址</param>
        ' <param name="_JBGW_PLC">接驳工位PLC</param>
        ' <param name="_RGV_PLC">RGV车PLC</param>

        ' pa = DirectCast(pa, thParam)
        writeInfoLog("开始等待下料完成》》》》》》》》》》》》》》》》")
        While True

            Try
                JBGW下料完成 = pa._JBGW_PLC.ReadBool(pa.DN_CMD).Content() '1#下料完成	Bool	DB17.27.4

                If pa._RGV_PLC.ReadBool("DB17.26.1").IsSuccess = True Then
                    CarDoorOpen = pa._RGV_PLC.ReadBool("DB17.26.1").Content() '门上升到位	Bool	DB17.26.1
                    If CarDoorOpen = False Then
                        writeErrorLog("下料过程中，车门异常关闭！！！")
                        setSysParam("RGV_AUTO_ERROR", RGV_ERROR_1)
                        canelTaskDetail()

                        'RGV急停
                        pa._RGV_PLC.Write(pa.scram_CAR, True)
                        '接驳工位 急停
                        pa._JBGW_PLC.Write(pa.scram_JBGW, True)
                        Return False

                    End If
                Else
                    writeErrorLog("下料过程中，车门信号读取错误！！！")
                End If

                'writeInfoLog("正在等待下料完成...:" & JBGW下料完成)
                If JBGW下料完成 Then
                    Dim finished_num As Integer = 0
                    writeInfoLog("接驳工位下料完成")
DOWN_RGV_finish_CMD_SEND: If pa._RGV_PLC.Write(pa.Car_DN_CMD, True).IsSuccess Then
                        writeInfoLog("发给RGV下料完成信号True 完成")
                        rtn = True
                    Else
                        If finished_num < 20 Then
                            writeErrorLog("发给RGV下料完成信号True 失败再发送：" & finished_num)
                            GoTo DOWN_RGV_finish_CMD_SEND
                            finished_num = finished_num + 1
                        End If
                        writeErrorLog("发给RGV下料完成信号True 失败")
                        rtn = False
                    End If
                    Thread.Sleep(500)
                    finished_num = 0
DOWN_RGV_finish_CMD_SEND2: If pa._RGV_PLC.Write(pa.Car_DN_CMD, False).IsSuccess Then
                        writeInfoLog("发给RGV下料完成信号False 完成")
                        rtn = True
                    Else
                        If finished_num < 20 Then
                            writeErrorLog("发给RGV下料完成信号False 失败再发送：" & finished_num)
                            GoTo DOWN_RGV_finish_CMD_SEND2
                            finished_num = finished_num + 1
                        End If
                        writeErrorLog("发给RGV下料完成信号False 失败")
                        rtn = False
                    End If
                    'pnlWait.Visible = False
                    Exit While
                End If

            Catch ex As Exception
                rtn = False
                writeErrorLog("@下料错误：" & ex.Message)

                Exit While
            End Try
            Thread.Sleep(500)
        End While
        Return rtn
    End Function

    Private Function tmUpWait_Elapsed(pa As thParam) As Boolean 'sender As Object, e As ElapsedEventArgs) Handles tmGJBUPWait.Elapsed

        Dim rtn As Boolean
        pa = DirectCast(pa, thParam)
        Dim _1上料完成 As Boolean
        Dim _2上料完成 As Boolean
        Dim StopCmd As Boolean
        Dim Current_StationNo As Integer
        Dim CarDoorOpen As Boolean
        If pa._rollerNo = "1#" Then
            Current_StationNo = 0
        Else
            Current_StationNo = 1
        End If
        Thread.Sleep(3000)
        writeInfoLog("等待上料完成》》》》》》》》》》》》》》》》")
        While True
            Try
                'seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                _1上料完成 = pa._RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                _2上料完成 = pa._RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                StopCmd = pa._RGV_PLC.ReadBool("DB16.4.4").Content()
                If pa._RGV_PLC.ReadBool("DB17.26.1").IsSuccess = True Then
                    CarDoorOpen = pa._RGV_PLC.ReadBool("DB17.26.1").Content() '门上升到位	Bool	DB17.26.1

                    If CarDoorOpen = False Then
                        Dim finish_num2 As Integer = 0
                        writeErrorLog("上料过程中，车门异常关闭！！！")
                        setSysParam("RGV_AUTO_ERROR", RGV_ERROR_1)

                        'RGV急停
                        pa._RGV_PLC.Write(pa.scram_CAR, True)


UP_JBGW_finish_CMD_SEND3: If pa._JBGW_PLC.Write(pa.DN_CMD, True).IsSuccess Then
                            Thread.Sleep(500)
                            pa._JBGW_PLC.Write(pa.DN_CMD, False)

                            writeInfoLog("上料完成end：滚轮" & Current_StationNo + 1)
                            rtn = True
                            Exit While
                        Else
                            If finish_num2 < 20 Then
                                writeErrorLog("接驳工位上料完成true 失败再发送：" & finish_num2)
                                GoTo UP_JBGW_finish_CMD_SEND3
                                finish_num2 = finish_num2 + 1
                            End If

                        End If

                        '接驳工位 急停
                        pa._JBGW_PLC.Write(pa.scram_JBGW, True)

                        canelTaskDetail()

                        Return False
                    End If
                Else
                    writeErrorLog("上料过程中，车门信号读取错误！！！")
                End If

                Dim Rollstop As Boolean = getCarRollerStopStatus(Convert.ToString(pa._RGV_PLC.ReadInt32("DB17.8.0").Content, 2), Current_StationNo)
                writeInfoLog(String.Format("RGV:1#上料完成:{0},2#上料完成:{1},滚轮{2}停:{3},急停:{4}", _1上料完成, _2上料完成, Current_StationNo + 1, Rollstop, StopCmd))
                Dim finish_num As Integer = 0
                If Current_StationNo = 0 Then

                    If _1上料完成 Or StopCmd Or Rollstop Then 'UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        writeInfoLog("上料完成Begin：滚轮" & Current_StationNo + 1)

UP_JBGW_finish_CMD_SEND: If pa._JBGW_PLC.Write(pa.DN_CMD, True).IsSuccess Then
                            Thread.Sleep(500)
                            pa._JBGW_PLC.Write(pa.DN_CMD, False)

                            writeInfoLog("上料完成end：滚轮" & Current_StationNo + 1)
                            rtn = True
                            Exit While
                        Else
                            If finish_num < 20 Then
                                writeErrorLog("接驳工位上料完成true 失败再发送：" & finish_num)
                                GoTo UP_JBGW_finish_CMD_SEND
                                finish_num = finish_num + 1
                            End If

                        End If
                    End If
                Else
                    If _2上料完成 Or StopCmd Or Rollstop Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        writeInfoLog("上料完成Begin：滚轮" & Current_StationNo + 1)

UP_JBGW_finish_CMD_SEND2: If pa._JBGW_PLC.Write(pa.DN_CMD, True).IsSuccess Then
                            Thread.Sleep(500)
                            pa._JBGW_PLC.Write(pa.DN_CMD, False)

                            writeInfoLog("上料完成end：滚轮" & Current_StationNo + 1)
                            rtn = True
                            Exit While
                        Else
                            If finish_num < 20 Then
                                writeErrorLog("接驳工位上料完成true 失败再发送：" & finish_num)
                                GoTo UP_JBGW_finish_CMD_SEND2
                                finish_num = finish_num + 1
                            End If
                        End If

                    End If
                End If
            Catch ex As Exception
                rtn = False
                writeInfoLog("@上料错误：" & ex.Message)

                pa._JBGW_PLC.Write(pa.DN_CMD, True)
                Thread.Sleep(500)
                pa._JBGW_PLC.Write(pa.DN_CMD, False)

                Exit While
            End Try
            Thread.Sleep(500)
        End While
        Return rtn
    End Function

    Public Function getCarRollerStopStatus(recStr As String, rollerFlg As Integer) As Boolean
        Dim r1 As Boolean = False
        Dim r2 As Boolean = False
        If recStr <> "" Then
            recStr = recStr.PadLeft(32, "0")
            Dim i As Integer = 2
            Dim array As Integer() = New Integer(16 - 1) {}
            Dim num As Integer = 0
            While i < 34
                array(num) = recStr.Substring(32 - i, 2)
                i += 2
                num += 1
            End While

            If array(8) = 10 Then
                r1 = False
            Else
                If array(8) = 1 Then
                    r1 = False
                End If
            End If

            If array(9) = 10 Then
                r2 = False
            Else
                If array(9) = 1 Then
                    r2 = False
                End If
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(1, 1) = "1" Then
                r1 = True '停
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(0, 1) = "1" Then
                r2 = True '停
            End If

        End If
        If rollerFlg = 0 Then
            Return r1
        Else
            Return r2
        End If
    End Function
    Private Function getStationName(_No As Integer) As String
        '接驳工位DataRow的行号  _1FKX_ROW,_1GKX_3_ROW
        Select Case _No
            Case 0
                Return "1FKX"
            Case 1
                Return "1FMS"
            Case 2
                Return "1GMS1"
            Case 3
                Return "1GKX_1"
            Case 4
                Return "1GMS1"
            Case 5
                Return "1GMS_3"
            Case 6
                Return "1GKX_3"
            Case 7
                Return "2FMX"
            Case 8
                Return "2FKS"
            Case 9
                Return "2GMX"
            Case 10
                Return "2GKS"
        End Select
        Return ""
    End Function

    Public Function car_send(plc_car As SiemensS7Net, _address As String, _value As Boolean) As Boolean
        Dim result As Boolean = False

        If plc_car.Write(_address, _value).IsSuccess() Then
            Threading.Thread.Sleep(500)
            plc_car.Write(_address, Not _value)
            result = True
        Else
            writeErrorLog(String.Format("RGV：{0} 写入失败,地址：{1} 值：{2}", plc_car.IpAddress.ToString, _address, _value))
            result = False
        End If

        Return result
    End Function

    Private Sub btnClearTask_Click(sender As Object, e As EventArgs) Handles btnClearTask.Click

        If taskNo = "" Then Exit Sub

        If MsgBox("确定要删除任务吗？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If clearTask() <> 1 Then
                MsgBox("删除失败")
            Else
                taskNo = ""
                lblTaskNo.Text = ""
                lblMemo.Text = ""
                dtTask.Clear()
                dtTask.AcceptChanges()
                Actioning = False

                FLB_LK_count_in_Car1 = 0
                FLB_LK_count_in_Car2 = 0
                GJB_LK_count_in_Car1 = 0
                GJB_LK_count_in_Car2 = 0
                'MsgBox("删除成功")
            End If
        End If
    End Sub

    Delegate Sub setJBGWStatusDet(_control As jbgw, _dr As DataRow, _car1_Loc As String, _car2_Loc As String)
    Private setJbgwStatusCaller As New setJBGWStatusDet(AddressOf setJBGWStatus)

    Delegate Sub setRGVStatusDet(_control As RGV, _dr As DataRow)
    Private setRGVStatusCaller As New setRGVStatusDet(AddressOf setRGVStatus)
    Private Sub setRGVStatus(_control As RGV, _dr As DataRow)
        _control.setStatus(_dr)
    End Sub
    Private Sub setJBGWStatus(_control As jbgw, _dr As DataRow, _car1_Loc As String, _car2_Loc As String)
        _control.setStatus(_dr, _car1_Loc, _car2_Loc)
    End Sub

    Private Sub tm_Elapsed(sender As Object, e As ElapsedEventArgs) Handles tm.Elapsed
        Dim a As String = ""
        With dtJBGWName
            a = ""
            If _Server_Run Then
                Try
                    Dim _car1_Loc As String = DBNULLtoZero(dtCarName.Rows(0)("CurrentLoc"))
                    Dim _car2_Loc As String = DBNULLtoZero(dtCarName.Rows(1)("CurrentLoc"))

                    'gw1FKX.setStatus(.Rows(_1FKX_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1FKX, .Rows(_1FKX_ROW), _car1_Loc, _car2_Loc)
                    a &= "1:"
                    'Thread.Sleep(100)
                    'gw1FMS.setStatus(.Rows(_1FMS_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1FMS, .Rows(_1FMS_ROW), _car1_Loc, _car2_Loc)

                    a &= "2:"
                    'Thread.Sleep(100)
                    'gw1GMS1_1.setStatus(.Rows(_1GMS1_1_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1GMS1_1, .Rows(_1GMS1_1_ROW), _car1_Loc, _car2_Loc)

                    a &= "3:"
                    'Thread.Sleep(100)
                    'gw1GKX_1.setStatus(.Rows(_1GKX_1_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1GKX_1, .Rows(_1GKX_1_ROW), _car1_Loc, _car2_Loc)
                    a &= "4:"
                    'Thread.Sleep(100)
                    'gw1GMS1_2.setStatus(.Rows(_1GMS1_2_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1GMS1_2, .Rows(_1GMS1_2_ROW), _car1_Loc, _car2_Loc)
                    a &= "5:"
                    'Thread.Sleep(100)
                    'gw1GKX_3.setStatus(.Rows(_1GKX_3_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1GKX_3, .Rows(_1GKX_3_ROW), _car1_Loc, _car2_Loc)
                    a &= "6:"
                    'Thread.Sleep(100)
                    'gw1GMS_3.setStatus(.Rows(_1GMS_3_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw1GMS_3, .Rows(_1GMS_3_ROW), _car1_Loc, _car2_Loc)
                    'Thread.Sleep(100)
                    'gw2FKS.setStatus(.Rows(_2FKS_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw2FKS, .Rows(_2FKS_ROW), _car1_Loc, _car2_Loc)
                    a &= "7:"
                    'Thread.Sleep(100)
                    'gw2FMX.setStatus(.Rows(_2FMX_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw2FMX, .Rows(_2FMX_ROW), _car1_Loc, _car2_Loc)
                    a &= "8:"
                    'Thread.Sleep(100)
                    'gw2GKS.setStatus(.Rows(_2GKS_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw2GKS, .Rows(_2GKS_ROW), _car1_Loc, _car2_Loc)
                    a &= "9:"
                    'Thread.Sleep(100)
                    'gw2GMX.setStatus(.Rows(_2GMX_ROW), _car1_Loc, _car2_Loc)
                    Me.Invoke(setJbgwStatusCaller, gw2GMX, .Rows(_2GMX_ROW), _car1_Loc, _car2_Loc)
                    a &= "10:"
                    'Thread.Sleep(100)
                    'Rgv1.setStatus(dtCarName.Rows(0))
                    Me.Invoke(setRGVStatusCaller, Rgv1, dtCarName.Rows(0))
                    a &= "11:"
                    'Thread.Sleep(100)
                    'Rgv2.setStatus(dtCarName.Rows(1))
                    Me.Invoke(setRGVStatusCaller, Rgv2, dtCarName.Rows(1))
                    a &= "12:"
                    'Thread.Sleep(500)
                Catch ex As Exception
                    writeInfoLog("6:" & ex.Message & "--" & a)
                End Try
            End If
        End With
    End Sub

    Private Sub dgvTask_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles dgvTask.RowPrePaint
        For i As Integer = 0 To dgvTask.Rows.Count - 1
            If dgvTask.Rows.Count > 0 Then
                If IsNothing(dgvTask.Rows(i).Cells("colStatus").Value) = False Then
                    If dgvTask.Rows(i).Cells("colStatus").Value.ToString = "2" Then
                        dgvTask.Rows(i).DefaultCellStyle.BackColor = Color.GreenYellow
                    ElseIf dgvTask.Rows(i).Cells("colStatus").Value.ToString <> "0" Then
                        dgvTask.Rows(i).DefaultCellStyle.BackColor = Color.Yellow
                    End If
                End If

            End If

        Next
    End Sub

    Private Sub btnReload_Click(sender As Object, e As EventArgs) Handles btnReload.Click
        setTaskAndTaskDetail()
    End Sub

    Private Sub ToolStripStatusLabel2_Click(sender As Object, e As EventArgs) Handles ToolStripStatusLabel2.Click
        If MessageBox.Show("是否关闭RGV自动控制服务？", "关闭"， MessageBoxButtons.OKCancel) = DialogResult.OK Then
            NotifyIcon1.Dispose()
            _Server_Run = False
            _Active_Run = False
            tm.Stop()
            DBacs.Disconnect()
            Dim isAlive As Boolean

            Try
                isAlive = th_Main.IsAlive
                If isAlive Then
                    th_Main.Abort()
                End If
            Catch ex As Exception

            End Try
        End If
        System.Threading.Thread.Sleep(3000)
        End
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
    End Sub

#End Region

End Class
